3

I have a class:

class test {
    function __construct() {
        print 'hello';
    }
    function func_one() {
        print 'world';
    }
}

what I would like to do is a have a class that sort of extends the test class. I say 'sort of', because the class needs to be able to run whatever function the test class is able to run, but NOT run the construct unless I ask it to. I do not want to override the construct. Anyone has any idea how to achieve this?

4
  • dont use a constructor then. there is no "sort of" extending. Commented Apr 2, 2010 at 3:53
  • @Patrick: it's not clear exactly what behavior you want. Perhaps if you stated your overall goal, it will be easier to understand. We also might think of a better approach. Commented Apr 2, 2010 at 18:12
  • As for "sort of extending", mixins and some of the techniques whytheluckystiff shows for mixins in advogato.org/article/470.html will "sort of" extend a class, though (since "sort of" is poorly defined) in a different way than Patrick means. Commented Apr 2, 2010 at 18:21
  • There are also the aggregate functions (php.net/manual/en/book.objaggregation.php) to combine classes (rather, add methods and properties of a class to an unrelated object) without subclassing in PHP4. In PHP5, there are the runkit functions (php.net/manual/en/ref.runkit.php). Commented Apr 2, 2010 at 19:06

3 Answers 3

5
class test {
    function __construct() {
        print 'hello';
    }
    function func_one() {
        print 'world';
    }
}


class test_2 extends test {
    function __construct() {
        if (i want to) {
            parent::__construct();
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

in any case, once you instantiate a new test object, constructor will be executed. i m sorry but this is perhaps misleading.
@user177883: In php you can omit the constructor of the parent class (simply by not calling it explicitly in the constructor of the derived class). I don't like it but that's the way it is.
1

What's wrong with overriding the construct?

class foo extends test {
   function __construct() { }
}

$bar = new foo(); // Nothing
$bar->func_one(); // prints 'world'

Comments

0

You could define a "preConstructor" method in your sub-classes that your root-class constructor would execute, and use a boolean flag to determine whether constructor code should be executed.

Like so:

class test
{
    protected $executeConstructor;

    public function __construct()
    {
        $this->executeConstructor = true;
        if (method_exists($this, "preConstruct"))
        {
            $this->preConstruct();
        }

        if ($this->executeConstructor == true)
        {
            // regular constructor code
        }
    }
}

public function subTest extends test
{
    public function preConstruct()
    {
        $this->executeConstructor = false;
    }
}

5 Comments

Coronatus' code is functionally equivalent and has less potential for error. However, this will execute the parent constructor by default whereas his won't. Which is preferable is up to Patrick.
@outis: one of the OP's requirements was "I do not want to override the construct." Coronatus' code is not functionally equivalent in that regard.
that reads more like part of Patrick's explanation of the phrase "sort of extend" and his idea of how it might work. For one thing, note the phrasing "do not want to" rather than "cannot". Even if that's not the case, it's a design constraint rather than a functional constraint. The ambiguity in the question makes it hard to know which answer is the better fit.
Any additional code that you put in preConstruct gets placed in Coronatus's test_2::__construct; as long as a coder properly follows your suggestion, the two approaches will produce the same output, and hence are functionally equivalent.
I suppose if some code uses ReflectionMethod::getDeclaringClass on the reflection of the constructor for a child class, you could get different results between your approach and Coronatus's, but (as there's no evidence the OP is using reflection and the chances are low that he is) that's about the only way. With every other operation (e.g. creating a new instance, invoking the constructor directly, referencing the constructor as a callback, checking that the method exists on the subclass), the two approaches are functionally indistinguishable.

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.