What's the difference between these 2 PHP codes?

4

I'm studying PHP and I came across the following question: what would be the difference between the following codes:

<?php
class ClasseTeste
{
    protected $db_host = 'localhost';
    protected $db_user = 'root';
    protected $db_pass = 'root';
    protected $db_name = 'bd_nome';

    public function hello()
    {
        echo $this->db_host;
    }
} 

And this:

<?php
class ClasseTeste
{
    protected function __construct()
    {
        $this->db_host = 'localhost';
        $this->db_user = 'root';
        $this->db_pass = 'root';
        $this->db_name = 'bd_nome';

        /**
         * Gostaria de saber se eu poderia tambem definir o tipo dos campos dentro da funcao __construct(). Dessa maneira:
         *
         * private $this->db_host = 'localhost';
         * private $this->db_user = 'root';
         * private $this->db_pass = 'root';
         * private $this->db_name = 'db_name';
         */
    }

    public function hello()
    {
        echo $this->db_host;
    }
}
    
asked by anonymous 14.05.2015 / 17:05

3 answers

5

The first code defines attributes explicitly as members of the class and their visibility also guaranteed ( protected ) all of them are accessible within the class and their descendants as expected.

The second defines the constructor as protected , that is, it can not be called directly, in this case it would be necessary for a static and public method to call the constructor and at the end return the desired object. This mechanic somewhat resembles the singleton pattern

The second code in the constructor class members are defined dynamically that at first look has the result of the first code but it is not so because the attributes created in this way have their visibility as public, ie they are accessible by any one and this violates the encapsulation.

What you can do in the second code, not the first one.

//nesse exemplo mudei o construtor para public para teste
$obj = new ClasseTeste();
echo $obj->db_pass .' - '. $obj->db_user;
    
14.05.2015 / 17:47
3

The only functional difference is whether it is public or protected . The rest is just an organizational matter. There are people who prefer to just start the variable outside the function and set the values in the __construct function. But it makes no difference.

  

I wondered if I could also define the type of fields within the __construct () function. This way:

    private $this->db_host = 'localhost';
    private $this->db_user = 'root';

No, in that case it would have to be out of function.

* Note: The function __construct must be public and it is not mandatory to declare before, it can be created inside it.

    
14.05.2015 / 17:19
1

The first code works as follows:

You have a class ClasseTeste with 4 protected attributes, these attributes referring to access to a database and public function that when the call prints the db_host attribute.

The second code works as follows: When the class is instantiated, it assigns the strings to each attribute and public function that when the call prints the db_host attribute. It works with caveats, it is not possible to define the stereotype (protect, private, public).

<?php
class ClasseTeste
{
 protected $db_host;
    protected $db_user;
    protected $db_pass;
    protected $db_name;

    protected function __construct()
    {
        $this->db_host = 'localhost';
        $this->db_user = 'root';
        $this->db_pass = 'root';
        $this->db_name = 'bd_nome';
    }

    public function hello()
    {
        echo $this->db_host;
    }
}
$objeto = new ClasseTeste();
$objeto->hello();
?>
    
14.05.2015 / 17:21