How to use static property of a class in another class in PHP?

Asked

Viewed 104 times

1

I am venturing into the world of POO in PHP and I came up with the following question.

Of the four ways I used in the code below, to assign the value of $prop1 of MinhaClasse to the variable $val within the method fazAlgumaCoisa() of OutraClasse, which would be the most appropriate in terms of performance and safety?

Note: please take into consideration that in addition to $prop1, I will have many other similar properties in the same code.

<?php

class MinhaClasse {

    static $prop1 = 'Valor 1';

    public static function set($name,$val)
    {
        self::$$name = $val;
    }

    public static function get($name)
    {
        return self::$$name;
    }

}


class OutraClasse {

    public $propA;
    public $propB;

    public function __construct()
    {
        $this->setProps();
    }

    public function setProps()
    {
        $this->$propA = MinhaClasse::$prop1;
        $this->$propB = MinhaClasse::get('prop1');
    }

    public function fazAlgumaCoisa()
    {

        // Abaixo Diferentes forma de obter "Valor 1" da "MinhaClasse".

        $val = MinhaClasse::$prop1; // 'Valor 1'
        $val = MinhaClasse::get('prop1'); // 'Valor 1'
        $val = $this->$propA; // 'Valor 1'
        $val = $this->$propB; // 'Valor 1'

        // o método fará algo à mais a partir daqui...

    }

}

1 answer

1


If you want performance and security I would review the idea of using PHP and OOP has nothing to do with it. You’re actually doing less OOP than you think. It’s common for people to say who they’re doing without understanding what OOP actually is, people think that OOP is put in a class, and it’s not.

It’s doing more metaprogramming to some extent than OOP and this makes performance worse, but not much because it already has natural costs when using classes. And when you use something static you’re giving up working with the object itself.

You are talking about an artificial example and OOP needs context, to understand the requirements and to model the object according to the need. In artificial examples none of this exists or accepts anything, which makes any solution right and wrong at the same time.

Static properties only exist in the class and not in the object, so if you’re thinking of inheritance that’s not possible and it doesn’t make sense.

Interestingly, you can do all that you are doing in PHP without the class in a much simpler way. PHP was originally created to have everything static and access members of a certain type through strings instead written code. Therefore, you are using the wrong tool and complicating what is simple. If you are going to do OOP, all this code is wrong, even if it works, if you really want to do what is in it, class is the inappropriate tool.

Can help:

  • I get it. I’m actually using this code more to learn POO (OOP) anyway, so I wondered what would be the most indicated way to get 'Value 1' within the context I mentioned, following the best practices, conventions, etc... Anyway thank you for the answer. I will analyze the links you mentioned. Obg

  • @robssanches If I told you that "POO is not code", what would you reply?

  • @Anderson Carlos Woss Now I would say that I believe you are right and that POO would not be the code itself, but rather the methodology or concept used behind the writing, to allow the organization and possibility of reusing the code. But I don’t know if I’m right in that definition...

  • @robssanches what concepts? And how your code relates to them?

Browser other questions tagged

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