Re: inheirtence/extends assistance

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



jeffrey_n_Dyke@keane.com wrote:

 > I'm trying to move some code to an OO model.  In the existing code, i 
have
 > a config.inc.php file which is `require`d to set up all the needed
 > variables/values used in later functions/pages.  Currently, they are all
 > stored in arrays(4) and those arrays are made global and imported into
 > functions.
 >
 > What i would like to do is have a base class which holds all the
 > configuration data, likely loaded by a method of that class to make for
 > easy user manipulation, but for now i'm hard coding the definitions to
 > understand the process.  I'm green to OO PHP, but am learning...or trying
 > to.
 >
[cut]

 > The config class does not need any methods, other then the 
constructor, but
 > only needs to be there to hold class variables.  All the examples 
that i've
 > found googling about 'extends' point to using methods of classes after
 > instantiating the subclass.  How do i access the parent class variables?


Well, using the setup you made, see the following:

<?php

class config {
    var $main_cfg = array();
    function __construct(){
       $this->main_cfg['install_root'] = '/path/to/install/directory';
    }
}

class action extends config {
    function action() {
       parent::__construct();
       print $this->main_cfg['install_root'];
    }
}

$act = new action();

?>

As you can see, I've made a constructor method in the config class, 
which is called from the constructor of the action-class (i named it 
__construct as per the php5 standard, in order to not have to get the 
classname of the parentclass for figuring out the constructorname, but 
this should work fine in php4 as well), you could also just populate the 
array in the class definition and then not have to call the constructor:

<?php

class config {
    var $main_cfg = array(
       'install_root' => '/path/to/install/directory'
    );
}

class action extends config {
    function action() {
       print $this->main_cfg['install_root'];
    }
}

$act = new action();

?>

But to answer your question, when you extend the config class in your 
action class, then you also extend its properties, therefore you can 
just access them by using $this->property in the extending class(action).

I'm not quite sure what you're hoping to gain by all these subclasses 
though, so I won't comment on what's the best solution for your needs, 
but at least now you know how to access the data.

If you wanted, you could later add a method to the config class:

function loadconfig($which, &$data){
    $this->{$which} = $data;
}

and then you could alter, like:

$act = new action();
$act->loadconfig('main_cfg', array('install_root' => '/new/path'));

And then you'd have the new values set..

-- 
Regards,

Nezar Nielsen
http://fez.dk




------------------------ Yahoo! Groups Sponsor --------------------~--> 
Yahoo! Domains - Claim yours for only $14.70
http://us.click.yahoo.com/Z1wmxD/DREIAA/yQLSAA/saFolB/TM
--------------------------------------------------------------------~-> 

PHP Data object relational mapping generator - http://www.meta-language.net/ 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/php-objects/

<*> To unsubscribe from this group, send an email to:
    php-objects-unsubscribe@yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Soap]     [Kernel Newbies]     [Yosemite]     [Yosemite Campsites]

  Powered by Linux