Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which is the better approach to initialize php properties?

Here are two way to initialize class variables.

1st Method

class Test {
    private $var1;
    private $var2;

    public function Test($var1,$var1) {
        $this->var1 = $var1;
        $this->var2 = $var2;
    }
}
$objTest = new Test("value1","value2");

2nd Method

class Test {
    private $var1;
    private $var2;

    public function _set($var, $value) {
        $this->$$var = $value
    }
}
$objTest = new Test();
$objTest->_set('var1','value1');
$objTest->_set('var2','value2');

Now, these both methods are valid, but I would like to know which one in better in what conditions? What are pros and cons of sticking with one method only?

like image 524
Starx Avatar asked Mar 16 '11 07:03

Starx


People also ask

Which method is used to initialise?

Answer: Constructor is the answer.

What are the properties of PHP?

In PHP, a property is qualified by one of the access specifier keywords, public, private or protected. Name of property could be any valid label in PHP. Value of property can be different for each instance of class. That's why it is sometimes referred as instance variable.

What is method and properties in PHP?

Methods are functions, so they can take arguments and return a value: $clan = $rasmus->family('extended'); PHP does not have the concept of private and public methods or properties. That is, there's no way to specify that only the code in the class should be able to directly access a particular property or method.


2 Answers

In your example, the second method is highly risky. If you give the variable name as an argument, you basically give the code the access to set all private variables from outside the class. What is the point of having private variables if you allow them to be set freely like that?

Additionally, the point of encapsulation in OOP, is that the inner workings of a class are not transparent to the code outside the class. Your second method breaks this encapsulation and thus part of the point of OOP, as the code outside the class has to be aware of the inner workings of the class, like the name of the variables. What happens if you later choose to change the variable names? All the code breaks. If they were accessed via setters/getters, old functions could be changed to reflect changes inside the class, but code outside the class would be difficult to change. In addition to that, the second method makes the validation of the values hard.

You should use the first method, especially if setting the class variables is necessary for operation. However, if you feel that some default values can be allowed for the attributes, you can just take advantage of PHP's default argument values like:

class Test {
    private $var1;
    private $var2;

    public function Test($var1 = 'defaultValue', $var1 = 'defaultValue') {
        $this->var1 = $var1;
        $this->var2 = $var2;
    }
}
$objTest = new Test();

Anyway, if the values must be initialized by the code, then you should definitely force them to be passed in the constructor. If default values are allowed, then either initialize the values in constructor with separate setters for the variables or just default argument values like in the provided example. It is, however, bad practice to expect the code to set critical values via setters after the constructor has been called.

like image 69
Riimu Avatar answered Sep 20 '22 22:09

Riimu


If those variables are necessary for the operation of the class, you would beter use the first method. That way you can ensure they are set when the class is created.

like image 37
Ikke Avatar answered Sep 19 '22 22:09

Ikke