P
P
Pavel Zamoroka2016-08-09 17:47:58
PHP
Pavel Zamoroka, 2016-08-09 17:47:58

Why is there no note?

In the constructor, I assign a value to the new property and everything works:

/**
     * Constructor function
     */
    protected function __construct()
    {
        parent::_construct();
        $this->_helper = 'blablabla';
    }

But as far as I understand, first you need to declare it:
/** @var  object $_helper */
    protected $_helper;

UPD: it's not magenta. Here is an example in pure php:
ini_set('display_errors', 1);
error_reporting(E_ALL);

/**
 * Class Foo
 */
class Foo
{
    /**
     * Constructor function
     */
    public function __construct()
    {
        $this->_bar = 'bar';
    }

    /**
     * Print bar
     */
    public function printBar()
    {
        echo $this->_bar;
    }
}

$foo = new Foo();
$foo->printBar();

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Melkij, 2016-08-09
@zapashok

If your class (or its ancestors) does not implement __set, then it is possible to add any new properties at runtime - they will be added as public.
A property declaration in a class definition is not really required by the language, but is required by other developers. And this is not just a good tone, namely, "it should be so."
And as an added bonus, if all the properties of an object are declared in the class definition, the objects of that class will use more efficient memory allocation. When trying to create a new property, it will be transparently migrated to a thicker structure.

L
Lander, 2016-08-09
@usdglander

Maybe there is a setter? In general, I would like to see the whole code :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question