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

PHP Refactoring - Lez. 1: a class

When we write each of us uses a class 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, 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 through the getXXX methods 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 be accessed 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, thanks to the magic functions can and __set __get.

If you 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-> $ name = _nomeClasse;

}

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 functions of the magical use of language.

The name we use outside the classroom is nothing more than property, but without the underscore as the first character. Since there is no class variable with that name, PHP __get or __set function throws, if any, in the first case, passing property name and value to be stored in the second case only the name.

Essential requirement for operation is that:

  • the dummy variable used outside of the object follows the naming convenction property inside the class, excluding dell'underscore that should not be present;
  • setXXX and getXXX methods exist for the property called;
  • the internal variable is easy to use, 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
*