We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Getting the parent class instead of the DI in Phalcon 3.0.0

Hi, here's my sample code.

Notice this line: $view->setViewsDir( $this->getViewDirectory( ) );

class Index extends \Phalcon\Mvc\User\Component Implements IndexInterface {

        protected $_id;
        protected $_name;
        protected $_type;
        protected $_path;
        protected $_directory;

        public function initialize ( ) {

        }

        public function registerAutoloaders ( ) {

        }

        public function registerServices ( $di ) {
                $di->set( 'view_simple', function ( ) use ( $di, $config ) {

                $view = new \Application\Libraries\Engine\View\ViewSimple( );

                $view->setViewsDir( $this->getViewDirectory( ) ); 
                get_class($this); // Phalcon 2.0.X it returns the Index Classs, Phalcon 3.0.0 returns DI

                $view->registerEngines( array(
                    '.volt' => function ( $view, \Phalcon\DiInterface $di ) use ( $config ) {

                        $volt = new \Phalcon\Mvc\View\Engine\Volt( $view, $di );
                        $volt->setOptions( array(
                            'compiledPath'      => $config->builds->path,
                            'compiledExtension' => $config->builds->extension,
                            'compiledSeparator' => '_',
                            'compiledAlways'    => $config->builds->always
                        ));

                        return $volt;

                    },
                    '.phtml'    => '\Phalcon\Mvc\View\Engine\Php'
                ));

                return $view;

            });
        }

        public function getViewDirectory ( ) {
                return ....
        }
}

How can i still access the Index class instead of the DI?

Imagine if i extended the class Index and the getViewDirectory method is returning differrent values.



85.5k

i am not an expoert of those but take a look at : https://blog.phalcon.io/

search page for Phalcon\Di is now bound to services closures allowing use



145.0k
Accepted
answer

$this is now bound to services. To make your work working i guess you possibly need to add user ($this) or save $this to some variable before setting service and use this variable in service definition.

edited Aug '16

It works but it's ugly... hahaha


class Index extends \Phalcon\Mvc\User\Component Implements IndexInterface {

        protected $_id;
        protected $_name;
        protected $_type;
        protected $_path;
        protected $_directory;

        public function initialize ( ) {

        }

        public function registerAutoloaders ( ) {

        }

        public function registerServices ( $di ) {
                $_this = $this;

                $di->set( 'view_simple', function ( ) use ( $di, $config, $_this  ) {

                $view = new \Application\Libraries\Engine\View\ViewSimple( );

                $view->setViewsDir( $_this->getViewDirectory( ) ); 
                get_class($_this); // it returns the Index Classs

                $view->registerEngines( array(
                    '.volt' => function ( $view, \Phalcon\DiInterface $di ) use ( $config ) {

                        $volt = new \Phalcon\Mvc\View\Engine\Volt( $view, $di );
                        $volt->setOptions( array(
                            'compiledPath'        => $config->builds->path,
                            'compiledExtension'   => $config->builds->extension,
                            'compiledSeparator' => '_',
                            'compiledAlways'  => $config->builds->always
                        ));

                        return $volt;

                    },
                    '.phtml'  => '\Phalcon\Mvc\View\Engine\Php'
                ));

                return $view;

            });
        }

        public function getViewDirectory ( ) {
                return ....
        }
}

I never checked but check only $this, it should override $this from Di i think.

Already tried it

Output: Fatal Error: Cannot use $this as lexical variable