Jump to content

Recommended Posts

Posted

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).

Posted (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 by flydev ??
  • Like 1
Posted

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;
  }

}
  • Thanks 1
Posted (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 by flydev ??
typo
  • Like 4
  • Thanks 1
Posted
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.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...