Solved

PHP OOP

Posted on 2009-05-16
7
189 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
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

932 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

8 Experts available now in Live!

Get 1:1 Help Now