Solved

PHP OOP

Posted on 2009-05-16
7
188 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
  • 3
  • 2
  • 2
7 Comments
 
LVL 2

Expert Comment

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

0
 

Author Comment

by:krydea
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 34

Expert Comment

by:Beverley Portlock
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now