Clarity Posted July 27, 2022 Share Posted July 27, 2022 Hello everyone! Can you please say how is this situation really possible? 1) On some line of the code inside a class, I assign the value: $this->var = $this->foo(). 2) On the next line of this code, I compare these two and output to TracyDebugger: bd($this->var == $this->foo());, but it outputs false value (first is null and $this->foo() is a set of pages). Link to comment Share on other sites More sharing options...
AndZyk Posted July 28, 2022 Share Posted July 28, 2022 Hello @Clarity, maybe $this is a reserved variable that ProcessWire uses internal: https://processwire.com/docs/start/api-access/ That is maybe the reason why your $this->var is overridden. I would avoid using $this or other reserved variables: https://processwire.com/docs/start/variables/ But maybe somebody else knows more for what $this is used. Regards, Andreas 1 Link to comment Share on other sites More sharing options...
Clarity Posted July 28, 2022 Author Share Posted July 28, 2022 Hello, @AndZyk! Yes, I think it might be the reason. Using $this->view in Wireframe instead of $this resolved my problem. 1 Link to comment Share on other sites More sharing options...
flydev Posted July 28, 2022 Share Posted July 28, 2022 (edited) Edit: Oh okay, WireFrame ? --- You should share the code of the class, meanwhile, you can try/compare with this : <? namespace ProcessWire; class Test extends Wire { public $foo1; public function foo2() { return $this->pages->get('/')->children; } public function run() { bd($this->foo1, 'foo1'); // null bd($this->foo2(), 'foo2()'); // set of pages bd($this->foo1 == $this->foo2(), 'comp 1'); // false $this->foo1 = $this->foo2(); bd($this->foo1, 'foo1 assigned'); // set of pages bd($this->foo1 == $this->foo2(), 'comp 2'); // true } } $test = new Test(); $test->run(); Edited July 28, 2022 by flydev ?? 1 Link to comment Share on other sites More sharing options...
Clarity Posted July 28, 2022 Author Share Posted July 28, 2022 Well, my code is like this: <?php namespace Wireframe\Controller; class Test extends \Wireframe\Controller { public function init() { $var = $this->foo(); $this->var = $this->foo(); $this->view->var = $this->foo(); bd($var); // 1 bd($this->var); // null bd($this->view->var); // 1 } public function foo() { return 1; } } 1 Link to comment Share on other sites More sharing options...
Pixrael Posted July 28, 2022 Share Posted July 28, 2022 class Test extends \Wireframe\Controller { public $var; ...... etc } maybe? 1 1 Link to comment Share on other sites More sharing options...
flydev Posted July 28, 2022 Share Posted July 28, 2022 (edited) 6 hours ago, Clarity said: Well, my code is like this... Then reason is the following: As you are declaring $this->var in the init() method, then you are declaring it in the earliest possible point of Wireframe execution, and only available after the instanciation (keep in mind that init() is called multiple times.), and then, the controller object set the $var in the $view scope which is a property of the controller, that's why the variable is available on $this->view object. A contrario, if you was declaring the variable as an object property (like @Pixrael just suggested) then the $this->var would have been available in your test's object scope, as the variable was declared as (Test) object property. Hope you get it. And guys correct me if I am wrong, thanks. Edited July 28, 2022 by flydev ?? typo 4 1 Link to comment Share on other sites More sharing options...
gRegor Posted July 28, 2022 Share Posted July 28, 2022 10 hours ago, AndZyk said: maybe $this is a reserved variable that ProcessWire uses internal: https://processwire.com/docs/start/api-access/ Not ProcessWire-specific. $this-> is the way to access properties and methods within the object, as part of Object Oriented Programming. More: https://www.php.net/manual/en/language.oop5.properties.php 2 1 Link to comment Share on other sites More sharing options...
Clarity Posted July 29, 2022 Author Share Posted July 29, 2022 14 hours ago, Pixrael said: class Test extends \Wireframe\Controller { public $var; ...... etc } maybe? Yes, it works too. Thank you for the advice. 14 hours ago, flydev ?? said: Then reason is the following: ... Hope you get it. And guys correct me if I am wrong, thanks. Let me think about it. Thank you about the explanation. ? 10 hours ago, gRegor said: Not ProcessWire-specific. $this-> is the way to access properties and methods within the object, as part of Object Oriented Programming. More: https://www.php.net/manual/en/language.oop5.properties.php Thank you for the addition. Link to comment Share on other sites More sharing options...
Recommended Posts