Solved

PHP OOP

Posted on 2009-05-16
7
191 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 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…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

830 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