Jump to content

Module extending non PW Class doesn't work anymore under 3.x


kixe
 Share

Recommended Posts

I have a custom module running under PW 2.7. The module class extends a third party class out of the PW scope. I want to make an update to 3.0 or 2.8. Unfortunately I cannot get it working.
Example:


Foo.module

<?php
require_once('bar.php');
class Foo extends Bar implements Module {
    public static function getModuleInfo() {
        return array(
            'title' => 'Foo',
            'summary' => 'Extending non PW Class'
        );
    }
}

 

bar.php

<?php
class Bar {
    public function aboutme() {
        return 'I am Bar. Parent class of Foo.';
    }
}

 

template.php

<?php
$foo = $modules->get('Foo');
var_dump($foo->aboutme());

 

I get the following results

No problems to install/ uninstall under PW 2.7. The return in frontend is as expected:
string 'I am Bar. Parent class of Foo.' (length=30)

Under PW 3.0 I get the following error:
Error: Class Foo contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (ProcessWire\Module::className)

I made the following update in Foo.module

<?php
require_once('bar.php');
class Foo extends Bar implements Module {
    public static function getModuleInfo() {
        return array(
            'title' => 'Foo',
            'summary' => 'Extending non PW Class'
        );
    }

    public function className($options = null) {
        return get_class($this);
    }
}


Now trying to install the module I get:
Session: Failed to construct module: \Foo - Wire::wire($o) expected WireFuelable for $o and was given Foo

and in frontend:
Fatal error: Call to a member function aboutme() on null

:( Why isn't it possible anymore to extend a class which is not an instance of Wire or WireFuelable? How could I get this working again? Any suggestions welcome!

 

Link to comment
Share on other sites

Multi-instance support does require modules to be wireable to a specific processwire instance. This is most easily done by extending any Wire classes. While it was possible in 2.x to just implement the Module interface it was never the suggested way to do that. For 3.0 you can always create a module, which extends a wire class and just forward all method calls to your Bar class.

Link to comment
Share on other sites

@LostKobrakai

Thanks for the reply. I know I could use magic methods like __call(), __set() and __get().
But what can I do if some of the properties in the bar class are protected? How can I set or get them via module, if the module isn't a child class of 'Bar'?

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

×
×
  • Create New...