What is the difference between these 2 PHP codes?

Asked

Viewed 161 times

4

I am studying PHP and 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;
    }
}
  • Some of these answers answered him?

3 answers

5

The first code explicitly defines the attributes as members of the class and their visibility is 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 cannot be called directly, in which case it would be necessary a static and public method that called the constructor and at the end returned the desired object. This mechanics reminds us a little bit of the Singleton standard

The second code in the constructor are defined class members in a dynamic way that at first glance has the result of the first code but is not so because the attributes created in this way have their visibility as public, that is, they are accessible by anyone and this violates the encapsulation.

What is possible to do in the second code and not first.

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

3

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

I wonder if I could also define the type of fields within the __Construct() function. That way:

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

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

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

1

The first code works as follows:

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

The second code works as follows: When the class is being instantiated it assigns the strings to each attribute and public function that when called prints the attribute db_host. 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();
?>
  • So if I’m going to use the __Construct() function, do I need to declare the variable outside the constructor, and define its value within __Construct()? The real function of __Construct() then is, at the moment the class is instantiated to assign a value, but only that? Correct?

  • Why doesn’t it work? I don’t have here how to test it, but I see no reason why it shouldn’t work...

  • __construct is the class constructor, you do not call it explicitly but when you instantiate a class ubject as I did in my example.

  • The attributes of the object were not defined, attributes that were being assigned within the constructor

  • Correction works however it is not possible to set the stereotype (Protect, private, public) will be set the default

  • was what it seemed to me, you do not have is the Scope defined as protected. but error does not give :)

  • But it’s a bad practice

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.