2

I have two classes that work seperate from another, but they extend the same class. Is it possible to have them work the same instance of the extended class. I'm wanting the constructor of the extended class to run only once.

I know this isn't right but something like this:

<?php
$oApp = new app;
class a extends $oApp {}
class b extends $oApp {}
2
  • I don't understand how a class can extend an object. Classes only extend other classes, don't they? Commented Apr 24, 2010 at 2:04
  • 1
    I know it can't, that's what I said. Just included the code as to what I want. Commented Apr 24, 2010 at 2:11

2 Answers 2

2

Ah, in that case I believe you would want to pass the class in as a parameter for the other two classes:

/**
 * 
 */
class abParent{
    /**
     * @var app
     */
    protected $app;
    /**
     *
     * @param app $app
     */
    public function __construct(app &$app){
        $this->app = &$app;
    }
}

class a extends abParent{}
class b extends abParent{}


$app = new app();
$a = new a($app);
$b = new b($app);

var_dump($a, $b);
Sign up to request clarification or add additional context in comments.

Comments

0

What you want is this:

<?php
$oApp = new app;
class a extends app{}
class b extends app{}

If you have __constructors in the child classes, make sure that they call parent::__constructor, otherwise they will probably not work properly.

1 Comment

That I get. Was curious if I'm missing on classes. What I'm trying to achieve is along the lines of having the app constructor only sending one email when two classes extend it instead of an email per extend.

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.