Cascading namespace in php with child classes

I am using the alias and names class in the parent class successfully, but it doesn't seem to be available in the child class. The actual error is from the autoloader. The weird thing is that the function works in the parent class and loads fine. How can I make the class represented use

available in subclasses?

edit: recipes are inactive - would it be wise to make them single in the base and then refer to them as elements in the child class MyTest?

I have two files:

Base.php:

namespace selenium;
use selenium\recipe\Cms as Cms;
class Base extends \PHPUnit_Framework_TestCase
{
    public function __construct()
    {
        Cms::staticfunc(); //works fine
    } 
}

      

MyTest.php:

class MyTest extends \selenium\Base
{
    public testMyTest()
    {
        Cms::staticfunc(); //errors here 
    }
}

      

+3


source to share


1 answer


From a comment:

i was hoping for a way to cascade using without duplicating this line among 20 or so child classes

This is one of the biggest problems I have encountered with PHP namespacing, which you have to call use

for every file the current script needs to reach. This is the same situation that we had to face when calling require_once 20 times in some scripts to bring in the required libraries.

What I prefer to do is namespace my files (as they are on a filesystem like Zend Framework) and use an autoloader to avoid all the mess. I am currently using ZF autoloader which can be used out of scope or you can also use PHP implementation using willa with SplAutoload .

- Update -



I have a library that I have written over the past few years called Hobis_Api, and it resides on the filesystem with the same convention; ~ / Projects / projects / dp / HOBIS / Library / HOBIS / Api / *. To register a namespace with Zend_Loader, I do the following:

// Be sure to set the include path to include the Zend and Hobis_Api files
// Not sure how your setup is, but would look something like:
set_include_path(get_include_path() . ':' . DIRNAME(__FILE__));

require_once 'Zend/Loader/Autoloader.php';

$autoloader = Zend_Loader_Autoloader::getInstance();
$autoloader->registerNamespace(
    array(
        'Hobis_Api_'
    )
);

      

Typically the above code ends up in some kind of bootstrap file that you can call from a centralized script to register the autoloader once.

Now, if your include path is set correctly, anytime you link to Hobis_Api_*

, it will be automatically loaded for you, so you don't need to call use

or require_once

, example usage:

// SomeScript.php

// Notice no requires

// I can make a call to Hobis_Api_Image without error
$image = Hobis_Api_Image;
$image->setHeight(400);

      

+2


source