Rick Kukiela
Rick Kukiela

Reputation: 1264

PHP create child class that is actual instance of parent class

OK - What I'm trying to do is kind of convoluted and I'm not sure if its even possible or if there is another "proper" way to do this but here it is in a nutshell:

class foo {

    var $testvar = "foo";

    function doTest() {

        echo $this->testvar . "\n";

        $t = new bar;
        $t->updateParent();

        echo $this->testvar;

    }

}

class bar extends foo {

    function updateParent() {
        $this->testvar = "bar";
    }

}

/*
What I get:
foo
foo

What I want:
foo
bar
*/

The reason I'm doing this is I'm designing a template engine and basically for my purposes the foo class is the main class that has the bulk of my application code. The system is designed so the users can create their own template php files which are loaded by the application within the context of a foo method. I want to set all the properties and methods of foo to private save for certain ones that will be protected and thus accessible to bar. The point being I want the users template php code to have access to only a limited number of functions of the parent class when I include their code.

A better example would be:

class foo { 
    protected $db;
    private $settings;

    function SomeAction() {

        // some code that results in a template needing to be loaded
        // code that determines the template file

        $template = new bar;
        $template->loadTemplate($file);
    }
}

class bar extends foo {

    function loadTemplate($file) {

        //if file exists
        require($file);
        // has access $db driver class (without creating a new instance of it)
        // does not have access to the $settings property

    }

}

Any Ideas?

Upvotes: 1

Views: 1975

Answers (4)

Mihai Răducanu
Mihai Răducanu

Reputation: 12585

Every object instance is also an instance of it's parent. You make all properties private in the parent and offer secured access through public methods. All public methods will be available to child classes as if they were their own, without access to private properties.

class foo {

    private $db;

    public function dbSelect() {

        return $this->db->select();// Example

    }

}


class bar extends foo {

    public function loadTemplate($file) {

        require($file);

        $selected = $this->dbSelect();

    }

}

Upvotes: 0

naseeba c
naseeba c

Reputation: 1050

try this

 <?php
class foo {

    var $testvar = "foo";

    function doTest() {

        echo $this->testvar . "\n";


        $R =$this->updateParent();

        echo $R;

    }

}

class bar extends foo {

    function __construct()
    {
         parent:: doTest();
    }
    function updateParent() {
         $testvar = "bar";

         return $testvar;

    }

}
 $t = new bar;

Instead of using the literal name of the base class in your code, you should be using the special name parent, which refers to the name of your base class as given in the extends declaration of your class

Upvotes: 0

Rajesh Kumar
Rajesh Kumar

Reputation: 93

It seems strange to me that you are extending the foo class just to give access of $db may be some more properties. but this doesn't make sense to me. You should pass the dependency to both classes.

class foo { 
    protected $db;
    private $settings;

    function SomeAction(bar $bar) {

        // some code that results in a template needing to be loaded
        // code that determines the template file
        $bar->loadTemplate($file);
    }
}

class bar {

    function loadTemplate($file, Gateway $db) {
        // use $db here
        //if file exists
        require($file);        

    }

}

Upvotes: 1

JKrunD
JKrunD

Reputation: 46

I believe you're looking for parent

Give this a try:

    class foo {

    var $testvar = "foo";

    function doTest() {

        echo $this->testvar . "\n";

        $t = new bar;
        $t->updateParent();

        echo $this->testvar;

    }

}

class bar extends foo {

    function updateParent() {
        parent::testvar = "bar";
    }

}

/*
What I get:
foo
foo

What I want:
foo
bar
*/

Upvotes: 0

Related Questions