A
A
Anton2016-10-04 20:41:46
PHP
Anton, 2016-10-04 20:41:46

PHP __get() magic method not executing, what should I do?

Hello!
I'm learning PHP, I've reached OOP, where auto video courses consider the magic methods __get () and __ set ()
The problem arose precisely with the __get () method, namely, the fact that it is not executed.
The author claims that if we refer to a non-existent property in php, then the property will be created automatically and will be displayed when accessing it.

class SomeClass{
}
$obj = new SomeClass();
$obj->param = 4;
echo $obj->param; // должно быть выведено 4

I enter the above code, the author displays a four, I do not.
I checked everything several times, it's hard to make a mistake here, because. just a few lines of code.
Further, the author claims that when redefining the magic methods __get(), __set(), you can work with non-existent class properties.
The author shows this with the following code:
class MyClass {
    function __set($n, $v){
        echo $n, ":", $v;
    }
    function __get($n){
        echo $n;
    }
  }

$odj = new MyClass();
$odj->param = 100;
echo $obj->param;

The author displays the value "param: 100param", but in a similar situation, I simply display "param: 100".
Those. it turns out a situation where, for some reason, the __get () method is not executed.
Here I understand that it may be in the settings of my local server, on which I am studying, but after reinstalling it, nothing happens anyway.
PHP version - 5.5
Tell me what to do?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DevMan, 2016-10-04
@AFatum

Tell me what to do?
at least fix odj to obj and turn on error output.

N
Nazar Mokrinsky, 2016-10-04
@nazarpc

Всё дело в том, что вы не показываете весь код.
Указанные примеры дают совершенно одинаковый результат на PHP 5.6-7.1RC3 и HHVM 3.10-3.14: https://3v4l.org/XE8WC

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question