0

I'm converting a serie of classes I've developed into using namespaces.
One of those clases has a dependency:

File A

namespace VENDOR
class A{
    static public function(){
        B::getInfo();
    }
}

File B

namespace VENDOR;
class B{
    static public function getInfo(){
       //some work
    }
}

Then, this belongs to a personal library that I will include into a project. Class B is susceptible to be extended:

namespace APPNAMESPACE
class SuperB extends \VENDOR\B{
    static public function getInfo(){
        //Some different work
    }
}

My intention is that VENDOR\A calls APPNAMESPACE\SuperB, but here is where I see I'm not designing my system well as obviously it will continue to call VENDOR\B.

Previously to this I could achieve this behaviour as I was not using namespaces at all but the classical class naming convention DIR_CLASS.php that allowed my autoloader (using composer now) to look in first in high priority APP folder and if not found in other folders with less priority (similar to kohana, Codeigniter and others).

So how do I approach this (obviously without hardcoding APPNAMESPACE\SuperB into VENDOR\A)

1
  • Possibly a case for composition rather than inheritence Commented Aug 16, 2014 at 10:53

1 Answer 1

1

Without state, this becomes tricky quicly indeed. Is there a reason this has to be static call? If at all possible, I favor (configurable, testable, swappable, testable) instances.

Something like this would be much easier:

namespace VENDOR
class A{
    protected $b;
    public function __construct(B $b){
        $this->b = $b;
    } 
    public function something(){
        $this->b->getInfo();
    }
}

... so you can add in what you like. Probably, the B type hint in the constructor should rather be an interface then a class definition, and you may have little control over what an outside vendor provides you with.

If we assume you cannot touch VENDOR's code, I see no reasonably working approach. If it needs to be static (and think long and hard as to why that is), and you can touch the code, something like this might work, but I don't like it as it's harder to check, debug and to enforce consistency in the classnames (OTOH, untested):

namespace VENDOR
class A{
    protected static $b = 'B';
    static public function something(){
        call_user_func(array(self::$b,'getInfo'));
    }
    static function useB($classname){
        if(!in_array('\VENDOR\B',class_implements($classname))){
            trigger_error("$classname does not implement \VENDOR\B", E_USER_ERROR);
        }
        self::$b = 
    }
}

Again, chose an interface rather then an implementation to check for. Though once again we might run into static:: / self:: problems if you want to extend \VENDOR\A further on down the line. This is 'kind-of' the functionality you for instance see @ $PDO->setAttribute(PDO::ATTR_STATEMENT_CLASS, 'A class that extends PDOStatement');, but you instantly already see that there it's called at an instance (and the fact it requires to extend rather then implement PDOStatement has more to do with the C-level implementation / functionality of that class).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.