Jump to content

init() vs __construct()


chrizz
 Share

Recommended Posts

this question can be also asked on stackoverflow, but I think supporting the local forum is much better ?

I discovered the following issue while using PHPunit tests to cover some module methods:

If I declare an array in the init() method of a module it's not accessible in another method called from the testcase. If I move the same array initialization to the __construct() method it's working as expected. I am not sure if this is an issue with PHPunit, PW or just a misunderstanding by me.

Is there anyone out there who has some more expert knowledge about this?

Thanks!

 

  • Like 1
Link to comment
Share on other sites

4 hours ago, chrizz said:

If I declare an array in the init() method of a module it's not accessible in another method called from the testcase. If I move the same array initialization to the __construct() method it's working as expected.

It's possible your method that tries to get the array is firing before init() has fired. The Module docs say:

Quote

init()

This method is called after __construct() and after any configuration data has been populated to the module.

So __construct() fires earlier.

Where you choose populate the array would probably depend on whether you need to get any custom module config at that point. If you don't need the module config then populate the array in __construct().

But init() is early enough for most cases too. An example:

<?php namespace ProcessWire;

class TestModule extends WireData implements Module {

    public static function getModuleInfo() {
        return array(
            'title' => "Test Module",
            'version' => 1,
        );
    }

    public $colours = array();

    public function init() {
        $this->colours = array(
            'Red',
            'Orange',
            'Yellow',
        );
    }

}

2018-09-20_105428.png.9066c14ca0616d3ebac24a27e07b1994.png

  • Like 1
Link to comment
Share on other sites

Quote

... Classes which have a constructor method call this method on each newly-created object ...

http://php.net/manual/en/language.oop5.decon.php

__construct() is called automatically. init() not.

Every property defined/ populated  with __construct() is accessible inside the class/ module and outside if public. If you define properties or change their values with init() you have no access inside a function of your class without calling init() from inside this function.

If you init a module the processwire way inside a template you get what you expect.

EXAMPLE

<?php namespace ProcessWire;

class TestModule extends WireData implements Module {

    public static function getModuleInfo() {
        return array(
            'title' => "Test Module",
            'version' => 1,
        );
    }

    public $colours = array();

    public function __construct() {
        $this->colours = array(
            'Blue'
        );
    }

    public function init() {
        $this->colours = array(
            'Red',
            'Orange',
            'Yellow'
        );
    }

    public function getColourArray() {
        return $this->colours;
    }

}

 

 

If you call Module/  Class via ProcessWire or directly inside your template you will get different results:

<?php namespace ProcessWire;
// INSTANTIATE VIA PW
var_dump($modules->TestModule->getColourArray()); // init() is called by PW
// array(3) { [0]=> string(3) "Red" [1]=> string(6) "Orange" [2]=> string(6) "Yellow" } 

// INSTANTIATE CLASS
$test = new TestModule(); // the PHP way, init() is not called
var_dump($test->getColourArray());
// array(1) { [0]=> string(4) "Blue" } 

// CALL INIT AND CHANGE RESULT
$test->init();
var_dump($test->getColourArray());
// array(3) { [0]=> string(3) "Red" [1]=> string(6) "Orange" [2]=> string(6) "Yellow" } 

 

 

  • Like 5
  • Thanks 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

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