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

Creating a compatible library with phalcon 1.3 and 2.0

Hello,

The Interface InjectionAwareInterface has changed between version 1.3.4 and 2.0 :

The method setDi now declare the type of the parameter passed (\Phalcon\DiInterface $dependencyInjector)

So I use a conditionnal class declaration for creating a compatible library with the 2 versions of InjectionAwareInterface :

abstract class _MyLib implements \Phalcon\DI\InjectionAwareInterface {
    protected $_di;
    protected function _setDi($di) {
        $this->_di=$di;
        //Other stuff
    }
    ...
}

if (Version::get()==="1.3.4") {
    class MyLib extends _MyLib {

        public function setDi($di) {
            $this->_setDi($di);
        }
    }
} else {
    class MyLib extends _MyLib {

        public function setDi(DiInterface $di) {
            $this->_setDi($di);
        }
    }
}

Is there-a cleaner solution ? without using a conditional declaration of the classes?



12.8k

When I migrate Phalcon to 2.0 I change it pernamently in my project. Ofcourse I had to upgrade phalcon in each of my environments but now I have shared deploymend process. In my opinion this is better option.



1.4k

Thank you for your answer Michael, but I am considering the case of a public library, used in the context of projects based on an unknown version of Phalcon (maybe 1.3.4 or 2.0). And I wanted to avoid the user (of the library) to do :

For Phalcon 1.3 :

$di->set("myLib",function(){
    $myLib= new MyLib1();
    return $myLib;
});

For Phalcon 2.0 :

$di->set("myLib",function(){
    $myLib= new MyLib2();
    return $myLib;
});

But this may be a bad choice,since Phalcon 2.0 is released, and not my library...