krydea
asked on
PHP OOP
Hello,
I have created an interface with the following methods:
newInstance()
setResultSet($rs)
etc.
I implemented the interface in three different classes: county, region and district.
I created a new class Container that implements an iterator and that contains an array of county, region or district. Now the problem is that I want to call the implementation dependent methods from: county, region or district and at some point I want to get one of the elements from the container.
My code gives me the error: Call to a member function getTableName() on a non-object;
I though the following should work, but it doesn't:
I have created an interface with the following methods:
newInstance()
setResultSet($rs)
etc.
I implemented the interface in three different classes: county, region and district.
I created a new class Container that implements an iterator and that contains an array of county, region or district. Now the problem is that I want to call the implementation dependent methods from: county, region or district and at some point I want to get one of the elements from the container.
My code gives me the error: Call to a member function getTableName() on a non-object;
I though the following should work, but it doesn't:
$cont = new Container(new County());
$rs = (County) Container.get($key);
with the class set up like:
class Container{
private $dataTypeClass;
private $Container=array();
public function __construct($dataType){
$dataTypeClass = $dataType;
}
public function get($key) {
$this->dataTypeClass->getTableName();
$fields = $this->dataTypeClass->queryFields();
$tableName = $this->dataTypeClass->getTableName();
$sqlSelect ="SELECT $fields FROM $tableName WHERE id=='$key'";
$rs = db->doSelect($sqlSelect);
$this->dataTypeClass->newInstance(); //clears the dataTypeClass
$this->dataTypeClass->setResultSet(rs);
return $this->dataTypeClass;
}
}
ASKER
Damn that's all:p I just started PHP. Java has generic classes. Does PHP support a nicer way to do stuff this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
It is nothing to do with generic classes. Inside a PHP class you MUST use the $this-> syntax to reference class properties and methods.
Mods:
I don't know why I have html above as I didn't type it. Please edit.
I don't know why I have html above as I didn't type it. Please edit.
pedro - using the rich text option on the editor is what did it. If you are going to include HTML it is best to stick to plain text.
I had a hilarious time some months back when I posted the HTML for a form in Rich Text .....
I had a hilarious time some months back when I posted the HTML for a form in Rich Text .....
ASKER
Thanks!!
$dataTypeClass = $dataType;
to
$this->dataTypeClass = $dataType;
and it should work.