Letting an object decide its class
===

I'm sorry I could not come up with a more descriptive title. My problem is the following. Assume that you have two classes A and B and you know that sometimes it may happen that some code tries to instantiate an object of type A when what is actually needed is an object of type B. The point is that **the code to decide which one is the right object naturally belongs to class A** and not to the client code.

In javascript (ok, js doesn't have classes, but it makes the point clear) you can simply do

    function A() {
        if(some_condition) {
            return new B();
        }
        //else we proceed to customize and return our object
    }

I want to do something similar in PHP. The best thing I can come up with is

    class A {
        private function __construct() {
            //whatever you need to do
        }

        public static function getInstance() {
            if(some_condition) {
                return new B();
            }
            else {
                return new A();
            }
        }
    }

The problem is that the client code will always have to know that A is special and you have to instantiate objects with a static method.

Is there a way to delegate to A the choice of the type of object to return in a seamless way?