Is it possible to access a static attribute without using self :: or ClassName :: in object-oriented PHP?

0

Having the following class called Pagina :

    class Pagina
    {

        public $_nombre = "Bienvido a tu casa";
        public static $_url ="tucasa.com";


        public function bienvenida()
        {
            echo $this->_nombre;
            echo "URL:".$this->_url;

        }

        public static function bienvenida2()
        {
            echo "URL:". self::$_url;
        }
    }

$pagina= new Pagina();

$pagina->bienvenida2();

This contains an attribute of the static type ( static ) and to access the attributes static I must use a static method.

My question is:

Is it possible to access the static attribute $_url , without directly using the variable with the method self:: , ie is there another method where it can be accessed by placing _url ? , or is it only possible using the self:: or in this case Pagina:: ?

    
asked by Victor Alvarado 07.06.2017 в 17:25
source

1 answer

1

In OOP there are two kinds of variables:

Those that are defined at the class level and those that are defined at the object level.

Class objects exist even if an object of that class has never been instantiated. This feature also causes the variable to be shared by all the objects that are installed. If an object changes the value, that value changes for all the objects that use it.

Those that are at the object level, are created when an object is instantiated and die with it. And unlike the previous ones, the value they take is individual for each instantiated object.

In php the way to refer to an object variable is with -> and for the class variables (which are defined as static ) with the name of the class and :: , or within the same class as self:: since that self refers to the same class.

As you can see, that modifier is to give context to the variable you are referring to, so I do not see how it would be possible not to use it if what is needed is to reference a class variable.

    
answered by 07.06.2017 в 17:47