Page 1 of 1

App Design - OOP/inheritance problems

Posted: Thu Nov 19, 2009 11:02 am
by vargadanis
Hi!

So I would like to create a modular website. This website would get the page and module information from a DB. Each page would have a number of modules that would be loaded when the page is displayed. Each module's controller file (PHP code) is in separate PHP files. I know I will have to include those files when I need them. But I have some scope or some other issue which I don't know how to solve. So here is what I have and what I would like:

Code: Select all

/* this class is responsible for retrieveing the module information
 * activate the modules, etc... 
 */
class pageController {
  protected $modules = array();
  protected $somevar;
  public function __construct(){
    $this->somevar = 'asdadasd';
    /* set up some vars, call some other functions */
    $this->getModuleInfo();
  }
  private function getModuleInfo(){
    /* ok, I know the modules and what files to include */
    require_once 'module_file.php';
    /* and now let's call the contructor on the module */
    $nameOfModule = 'nameOfModule';
    $m = new $nameOfModule();
  }
}
$var = new pageController();
And the module class:

Code: Select all

 
class nameOfModule extends pageController {
  public function nameOfModule(){
    /* and here is the problem! */
    var_dump( $this->somevar );
    /* the output is: null 
     * and what I want is the actual value of the pageController::$somevar;
    */
  }
} 
I thought that if I use extend keyword I will be able to access the methods/properties of the parent class. Maybe I screwed something up?
I know that require and include will make the included file's scope to the scope where the file is included. Do you think it's got something to do with it?

Well, thanks for the help in advance anyway...

Re: App Design - OOP/inheritance problems

Posted: Thu Nov 19, 2009 11:35 am
by iankent
$nameOfModule isn't set in the first part, so you're trying to create a class with no name. Either set that variable, or just make it

Code: Select all

new nameOfModule()
instead.

edit: if you want the class name to be dynamic, which makes sense, then you need to set the variable $nameOfModule = 'nameOfModule' for your demo class

Re: App Design - OOP/inheritance problems

Posted: Thu Nov 19, 2009 1:33 pm
by vargadanis
Yeah I have set the var to actually contain the name of the class.
So the above example code should work? (ofc with the var modification)

Re: App Design - OOP/inheritance problems

Posted: Thu Nov 19, 2009 1:38 pm
by iankent
I think so yes, can't see why not

Re: App Design - OOP/inheritance problems

Posted: Thu Nov 19, 2009 1:44 pm
by vargadanis
Well It just doesn't... I mean when I try to access some of the methods or properties of the parent class all I get is null. So I don't know how to fix that.
2nd. code snippet in the original post, line 5-7 tells this problem.
Any tips how to fix that?

Re: App Design - OOP/inheritance problems

Posted: Thu Nov 19, 2009 1:58 pm
by iankent
Hm, it shouldn't be public function $somevar, just public $somevar in your pageController class

also, unless its in another bit of code somewhere, nothing is calling the nameOfModule() function within the nameOfModule class. you're creating an object of type nameOfModule in the constructor, but not doing anything with it at all

Re: App Design - OOP/inheritance problems

Posted: Thu Nov 19, 2009 2:23 pm
by requinix
Two problems:
1. The parent controller has a "__constructor" function. That's the wrong name - it should be "__construct".
2. The child constructor will not automatically call the parent constructor. You have to do that yourself with parent::__construct(). This probably needs to happen before anything else.

In PHP 5 using the class name as the constructor (like in PHP 4) is okay, but using __construct is preferred.

Re: App Design - OOP/inheritance problems

Posted: Thu Nov 19, 2009 3:01 pm
by vargadanis
Ok, I fixed a few things but I don't think I have made myself clear.

That 2nd class might not even have to extend the parentClass, the parent-child relationship might not even be necessary. What I would like to be able to do is: access the methods and properties of a class instance that has been already created.
So first I create an instance of parentClass which does a few things. One of those things is that it creates an instance of another class, of nameOfModule class in this example. Now what I would like to be able to do is to access the properties and methods of the instance that called the creation of the nameOfModule class.

Ehhh complicated is what I want... Hard to explain :)

Re: App Design - OOP/inheritance problems

Posted: Thu Nov 19, 2009 3:44 pm
by iankent
You don't necessarily have to inherit the class from the one instantiating the class if you don't want to. In fact, in this case it looks pretty odd!

However, once you've created the class using $obj = new $className(); you should be able to access $obj's properties and methods, as long as you know what they are!!