1

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?

2
  • your trying to access $settings without changing its Visibility? Commented Feb 19, 2016 at 5:40
  • no im not trying to access settings at all, however if the user code inside the template file tried to access $this->settings i do not want them to be able to. Commented Feb 19, 2016 at 5:44

4 Answers 4

1

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);        

    }

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

2 Comments

Well that was my first thought too, however there is a lot more in the class I want to be available to bar from foo... I just didnt go into that detail here. but Imagine foo has 50 methods and 20 properties and I want bar to have access to 20 methods and 16 properties of foo. Thats why I was hoping there was a way to just make bar an extension of foo and use protected / private to control access...
Yeah there is really no good way to do this with child / parent class set up so Im gonna figure out away to just do two separate classes.
0

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
*/

1 Comment

I get: Parse error: syntax error, unexpected '=' in the parent:: line and my ide suggests that attempting to set a constant. If I change it to parent::$testvar I get: Fatal error: Uncaught Error: Access to undeclared static property: foo::$testvar
0

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

1 Comment

This wouldn't exactly work for me because the application is running within the context of the initial foo object, and creating a bar object to essentially jail the user code its "including"... I think maybe an extended class is not the way to go here and rajesh has the right idea.
0

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();

    }

}

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.