Pixelairport's avatar

Extend a class without namespace

I have this code:

final class GoSomewhere extends NF_Abstracts_Action

Problem is, that I normally work with laravel, where everything has namespaces. That is why i also try to do this in wordpress with my plugin. But when I do this, my autoloader tries to load NF_Abstracts_Action from my own Namespace. The NF_Abstracts_Action has no namespace... can I require it and extend it... but how?

0 likes
11 replies
thinway's avatar

Have you tried with require and require_once PHP expressions?

Maybe, something like this:

require_once("YourClass.php"); // Here with the path to YourClass.php
class AnotherClass extends YourClass 
{
   ...
}
1 like
martinbean's avatar
Level 80

@pixelairport The class will exist in the “global” namespace, so you either need to import it as is, or prepend a backslash to the class name when extending it:

use NF_Abstracts_Action;

final class GoSomewhere extends NF_Abstracts_Action
{
    //
}

Or:

final class GoSomewhere extends \NF_Abstracts_Action
{
    //
}
2 likes
Pixelairport's avatar

Thx all for help. I already tried everything of this... i also checked if the class is there:

if(class_exists('NF_Abstracts_Action')) {
    die('hello');
}
// => Outputs hello

... and it is loaded. But when I do

 final class MultiMailjet extends NF_Abstracts_Action

i got: Fatal error: Uncaught Error: Class 'MyPlugin\Actions\NF_Abstracts_Action' not found in.

When I do

 final class MultiMailjet extends \NF_Abstracts_Action

i got: Warning: require_once(Actions/Ninja_Forms.php): failed to open stream: No such file or directory

...

I think the problem is, that autoloading tries to load it, even the class already exists. I got an error when doing the following code, that class "Myplugin\Actions\NF_Abstracts_Action" is not found even it seems class is loaded.:

if(class_exists('NF_Abstracts_Action')) {
    $test = new NF_Abstracts_Action();
}

I have no idea what else can i test/try now.

jlrdw's avatar

Have you tried a:

composer dumpautoload

Otherwise you may have to require the class.

Edit: Also check Wordpress forum.

1 like
Pixelairport's avatar

Thx. I checke this. Still dont work. The autoloader is setup in my new wordpress plugin and i extend a plugin which dont use namespaces. Is there a way to do something like alias in laravel config? ... but alias also use namespaces. I want to extend a class which has no namespace.

jlrdw's avatar

You will probably have to use @thinway answer, but I usually just require. If you use that, you need whole path.

Pixelairport's avatar

That is what i did first and I use the whole path. Without the path there would be another error. The file is loaded.

require_once (WP_PLUGIN_DIR.'/ninja-forms/includes/Abstracts/Action.php');
$myclass = new NF_Abstracts_Action();

This throws an error. The Actions.php is really a abstract class: "abstract class NF_Abstracts_Action"

Autloader seems to try load the class again. Could this be the error?

I have this to autoload my plugin files:

function plugin_mailjet_spl_autoloading($class_name)
{
    if (false !== strpos($class_name, 'MyPlugin')) {
        $class_name = str_replace('MyPlugin\', "", $class_name);
        $class_name = str_replace('\', DIRECTORY_SEPARATOR, $class_name);
        require_once($class_name . ".php");
    }
}
spl_autoload_register('plugin_mailjet_spl_autoloading');
Pixelairport's avatar

What do you mean with "initiate"? In wordpress there is no autloading. Also most plugins dont have autoloading. So i decided to use it for my plugin. And my plugin tries to load a class from another which dont have autoloading. But my plugin is in a autoloading context. So also when I extend the abstract class, php tries to autoload it again and looks at my autoloading.

Pixelairport's avatar

... im a step closer... i have to do the thing like @martinbean said... sorry... I have also to load other classes, because abstract class loads other, which sounds familiar. That is why i everytime thought it does not work. But extends works and throws an error because of similar class names which are required. Thx everyone for help.

Please or to participate in this conversation.