Solved

PHP OOP

Posted on 2009-05-16
7
193 Views
Last Modified: 2013-12-12
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

0
Comment
Question by:krydea
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 2

Expert Comment

by:pedro_sland
ID: 24402126
Change
                $dataTypeClass = $dataType;
to
                $this->dataTypeClass = $dataType;
and it should work.

0
 

Author Comment

by:krydea
ID: 24402173
Damn that's all:p I just started PHP. Java has generic classes. Does PHP support a nicer way to do stuff this?
0
 
LVL 2

Accepted Solution

by:
pedro_sland earned 250 total points
ID: 24402426
I realise that there is another error in your code:
Container.get($key);
Instead use:
$cont->get($key);

This would also give an error (probably the one you reported above):
Container::get($key);

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.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24402435
It is nothing to do with generic classes. Inside a PHP class you MUST use the $this-> syntax to reference class properties and methods.
0
 
LVL 2

Expert Comment

by:pedro_sland
ID: 24402436
Mods:
I don't know why I have html above as I didn't type it. Please edit.
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24402517
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 .....
0
 

Author Closing Comment

by:krydea
ID: 31582196
Thanks!!
0

Featured Post

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

689 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question