Home > Refactoring > Refactoring PHP - Lez. 2: array property of a class

PHP Refactoring - Lez. 2: array property of a class

Today we will discuss the properties consist of a class or property containing an array.

We saw in the previous lesson ( PHP Refactoring - Lez. 1: properties of a class ) how to write simple properties of a class.

As regards the properties composed apply the rules of naming convention introduced in the previous tutorial, except the methods of access to the same. Below the standard to follow at the class level.

Method addXXX

To add an item to a property made ​​use a method with the following signature:

public function addXXX ($ value, $ key = null)

Where XXX should be replaced with the name of the property.

The following is an example:

class Class {

$ private pupils;

public function addAlunni ($ value, $ key = null) {

if (is_array ($ value)) {

foreach ($ values ​​as $ pos => $ item) {

if ($ key! = null) {

$ This-> students [$ position] =

$ Element;

Else {}

$ This-> students [] = $ element;

}

}

} Else if ($ key == null) {

$ This-> students [] = $ value;

Else {}

$ This-> students [$ key] = $ value;

}

}

}

As you can see the parameters to pass are two: the first compulsory, the second optional.

As we go into addXXX method checks if the $ value is an array. if so loop through on the latter, and if the $ key parameter is null we queue elements in the property, but if $ key is not null insert value $ item in $ key position.

If $ value is an array and $ key is equal to null insert value directly in the $ property $ pupils.

Finally the last case is if the $ key parameter is enhanced, in which case insert value at $ $ $ key properties of pupils in the class.

Method removeXXX

To remove an item from a property made ​​use a method with the following signature:

public function removeXXX ($ key = null)

Where XXX should be replaced with the name of the property.

The following is an example:

class Class {

$ private pupils;

public function removeAlunni ($ key = null) {

if (is_array ($ key)) {

foreach ($ position as $ key => $ value) {

unset ($ this-> students [$ value]);

}

} Else if ($ key == null) {

$ This-> students = array ();

Else {}

unset ($ this-> students [$ key]);

}

}

}

As we go into removeXXX method is checked if the $ key parameter is an array. if so loop through on the latter, and we eliminate all the elements to the positions corresponding to the values ​​contained in the parameter of the method.

If $ key is an array and is equal to the $ null resetteremo pupils.

Finally the last case is if the $ key parameter is enhanced, in which case the value will delete the corresponding position in the property $ $ key pupils in the class.

GetXXX method

To retrieve items from a property made ​​use a method with the following signature:

public function getXXX ($ key = null)

Where XXX should be replaced with the name of the property.

The following is an example:

class Class {

$ private pupils;

public function getAlunni ($ key = null) {

if (is_array ($ key)) {

$ Elements = array ();

foreach ($ position as $ key => $ value) {

$ Elements [$ value] = $ this-> students [$ value];

}

return $ items;

} Else if ($ key == null) {

return $ this-> children;

}

return $ this-> students [$ key];

}

}

As soon as we enter the getXXX method is checked if the $ key parameter is an array. if so loop through on the latter, and eventually return an array containing the values ​​of the elements.

If $ key is an array and is equal to null will return all property $ pupils.

Finally the last case corresponds to the parameter value to $ key, in which case we will return the value corresponding to the position in the property $ $ key pupils in the class.

We close the lesson by assembling all the code written so far and applying it to a small example, clarifying:

  1. No comments yet ...
  1. No trackbacks yet ...
Immagine CAPTCHA
Audio CAPTCHA
Change image
*