Home > Refactoring > Refactoring PHP - Lez. 1: properties of a class

PHP Refactoring - Lez. 1: properties of a class

When we write a class we each use of its conventions.

I recommend using the following properties of a class:

  • If consists of a single word, all lowercase, for example $ name;
  • If made ​​of many words the first letter of each, starting from the second, in uppercase, all lowercase other, for example $ nomeAziendaItaliana;
  • If a constant is composed of single-word write in all caps, for example ELEMENTS;
  • If we have a constant composed of several words to write in all caps with a hyphen between the words, for example ELEMENTI_PER_RIGA
  • The variable name must be self-explanatory

All properties of a class are private and can be accessed via methods getXXX and setXXX, whose level of access must be public, so that you can access the internal state of the object from any point. Instead of XXX we put the name of the property.

The following is an example to better clarify the concept:

Wrong Exact

class Test {

public $ classname;

private $ mia_x1;

costante_della_classe const;

const pages;

}

class Test {

private $ className;

private $ miaDataDiNascita;

COSTANTE_DELLA_CLASSE const;

PAGES const;

public function setNomeClasse ($ name) {

$ This-> className = $ name;

}

getNomeClasse public function () {

return $ this-> className;

}

}

But because we do all this? The properties of a class represent the state of the instance of an object, then you should only have access via the public methods, deputies to trigger actions for change.

A real example is the light bulb: we are interested in their status, or hot or cold, but we act on it by the actions on and / or off, which represent our methods.

With regard to access properties in PHP 5 we use a trick, made ​​possible thanks to the magic functions __ set and __ get.

If we do not want to use directly in your code and the functions getXXX setXXX we can do as the following example:

class Test {

private $ _nomeClasse;

public function __ set ($ name, $ value) {

call_user_func (array ($ this, 'set'. ucwords ($ name)), $ value);

}

public function __ get ($ name) {

call_user_func (array ($ this, "get". ucwords ($ name)));

}

public function setNomeClasse ($ name) {

$ This-> _nomeClasse = $ name;

}

getNomeClasse public function () {

return $ this-> _nomeClasse;

}

}

$ Obj = new Test ();

$ Obj-> className = 'test';

echo $ obj-> className;

The trick is to follow a certain nomenclature in the names and enjoy the magic of language functions.

The name we use outside of class is just property, but without the underscore as the first character. As there is a class variable with that name, launches the PHP function __ set or __ get, if any, passing the former property name and value to be stored in the second case only the name.

Essential requirement for the operation is that:

  • the dummy variable used outside the object follow the naming convenction of the internal property to the class, to the exclusion dell'underscore that must not be present;
  • exist methods setXXX and getXXX for the property callback;
  • the internal variable to use is simple, as with an array operation will not be as you wish.

If the array we will see what methods to use in a future article.

Thanks to Henry for some useful suggestions in the drafting of the code.


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