Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Problems with 'onConstruct'

I have a base model Modelo, that has its onConstruct():

protected function onConstruct($data = array()) // I also tried with a 'public' modifier.

and a Grade model that extends from the previous one. My question is, why this isn't working, whenever I put the constructor in the first or second model?

// The 'var_dump()' of the construct method tells 'array(0) {}', wich isn't correct.
$data = array(
    'field' => 'value'
$grade = new Grade($data);

// This one var dumps the array correctly.


The constructor of a model does not receive an array as first parameter:

final public __construct ([Phalcon\DiInterface $dependencyInjector], [Phalcon\Mvc\Model\ManagerInterface $modelsManager])


Yeah, I know, but I'm referring to this (fourth block of code)


Yes, but there's no way to pass the array as parameter to onConstruct because the constructor does not receive an array as parameter as I mentioned before

Yeah, I know, but I'm referring to this (fourth block of code)

edited Jan '16

So basically, if you extend any Phalcon class which uses \Phalcon\DiInterface as a glue, onConstuct() method will work out of the box without arguments being defined.

In raw PHP this must be defined as:

    function __construct($routerGroup = null)
        $method = __FUNCTION__; //dynamically call parent method with the same name
        $this->setHttpMethods = ['GET', 'POST']; //Sets a set of HTTP methods that constraint the matching of the route
        parent::$method($routerGroup); //Pass $routerGroup to the parent constructor in order to builed the object

While in Phalcon extended child class it is as simple as:

class RouterGroup extends \Phalcon\Mvc\Router\Group{
//methods... etc 

$myGroup = new RouterGroup(['controller' => 'mycontroller']);

Child class will receive array as argument to the constructor, which is automatically passed to the parent without need to define it manually, or to call parent::onConstruct(); method.