Link to home
Start Free TrialLog in
Avatar of krydea
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:
$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;	
	}
}

Open in new window

Avatar of pedro_sland
pedro_sland
Flag of United Kingdom of Great Britain and Northern Ireland image

Change
                $dataTypeClass = $dataType;
to
                $this->dataTypeClass = $dataType;
and it should work.

Avatar of krydea
krydea

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
Avatar of pedro_sland
pedro_sland
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Beverley Portlock
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.
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 .....
Avatar of krydea

ASKER

Thanks!!