Solved

PHP OOP

Posted on 2009-05-16
7
192 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

734 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