Parent class returns instance of child?
Posted: Fri May 19, 2006 9:24 am
I'd appreciate it if someone could take a look at my code and offer some advice as to where I'm going wrong.
Here's the code...
Config class is an extension of my DataObject class (similar to the one from Maugrim's tutorial on this site.
The Config_Extra class I am using to try to dynamically add additional methods to the Config class without needing to instantiate the Config_Extra class directly in my front end code. Rather I'd like the constructor for the Config class to determine whether there are any extras to include and sort it out there.
When I try the following ...
I get the error ...
Can anyone offer any advice on this as it's driving me crazy.
Thanks.
Here's the code...
Config class is an extension of my DataObject class (similar to the one from Maugrim's tutorial on this site.
Code: Select all
<?php
class Config extends DataObject {
function Config($fields=null) {
// If the "_Extra" file exists this is where it will be located.
$extra_file = BASE_DIR . '/models/' . $this->className . '_Extra.class.php';
// Check if it exists. If it exists check if it has already been used.
if((file_exists($extra_file)) && (!isset($this->Extra))){
require_once($extra_file); // require the extra class
$class = $this->className.'_Extra'; // build the class name
$instance &= new $class(); // instantiate the _Extra class
return $instance; // Return the instance instead of this class
} else {
// This section will be called if this constructor is called from the _Extra class
parent::DataObject($fields); // Run this class' parent constructor
// Set some stuff
$this->tableName = 'config';
$this->className = 'Config';
$this->primaryKey = 'id';
}
}
function getId() { return $this->data['id']; }
function setId($var) { $this->data['id'] = $var; $this->changedFields[] = 'id'; }
function getValue() { return $this->data['value']; }
function setValue($var) { $this->data['value'] = $var; $this->changedFields[] = 'value'; }
}
?>The Config_Extra class I am using to try to dynamically add additional methods to the Config class without needing to instantiate the Config_Extra class directly in my front end code. Rather I'd like the constructor for the Config class to determine whether there are any extras to include and sort it out there.
Code: Select all
<?php
class Config_Extra extends Config {
var $Extra = true; // Defines if this file has been used yet or not.
function Config_Extra($fields=null){
// Call base constructor
parent::Config($fields);
}
// this is the extra method I want to dynamically include in the parent class
// by just instantiating the parent class in my front code.
function getTagLine(){
$this->setId('tag_line');
$this->getByPk();
return $this->data['value'];
}
}
?>Code: Select all
<?php
include(BASE_DIR.'/models/Config.class.php');
$test = new Config();
echo $test->getTagLine();
?>Code: Select all
Fatal error: Call to undefined method Config::getTagLine() <snip>Thanks.