• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:



I have created an interface with the following methods:

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) {
		$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
	return $this->dataTypeClass;	

Open in new window

  • 3
  • 2
  • 2
1 Solution
                $dataTypeClass = $dataType;
                $this->dataTypeClass = $dataType;
and it should work.

krydeaAuthor Commented:
Damn that's all:p I just started PHP. Java has generic classes. Does PHP support a nicer way to do stuff this?
I realise that there is another error in your code:
Instead use:

This would also give an error (probably the one you reported above):

The way that you are doing that is probably the most generic way. You could subclass the container class and make the County class extend the Container class. Use parent::get($key) if you also have a get() method in your County class. This would mean that a similar thing could be achieved with $rs = new County()->get($key).

Here are some tips that you may find helpful:

  • For mysql __destruct() is useful for closing the mysql connection and/or (more importantly) $mysqliResult->free_result().
  • In OOP with PHP it is useful to use the mysqli extension instead of mysql and $mysqliResult->fetch_object()
  • Sometimes it is useful to use $miscObject = new StdClass(). This will give you an empty object that you can use like $miscObject->public_var = 'whatever'.
Also, so that you know coming from Java, (County) - type casting - is not required in PHP.
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Beverley PortlockCommented:
It is nothing to do with generic classes. Inside a PHP class you MUST use the $this-> syntax to reference class properties and methods.
I don't know why I have html above as I didn't type it. Please edit.
Beverley PortlockCommented:
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 .....
krydeaAuthor Commented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now