Jump to content


Redefining PHP Classes


5 replies to this topic

#1 Lang

    Young Padawan

  • Members
  • Pip
  • 198 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 12 July 2009 - 02:35 PM

Hey there,

I have a class A that uses class B. Class B also uses class A. This is recursive and am wondering if you can redefine PHP classes like in C++.

class A{}

class B{
	private $classA;

	public function __construct($classA){
	   $this->classA = $classA;
	}

	public function someFunc(){ echo 'blah'; }
}

class A{
	private $classB;

	public function __construct($classB){
		$classB->someFunc();
	}
}

Thanks in advance,
Lang

#2 dotbart

    Young Padawan

  • Members
  • Pip
  • 141 posts
  • Gender:Male
  • Location:Diepenbeek
  • Interests:Webdesign, Webdeveloppement, DJ, ...

Posted 12 July 2009 - 03:48 PM

Hm, redefine classes?
You can use inheritance perfectly but I can't really think of an example of redefining a class..

Maybe explain what you want to achievo/do and we'll see from there.


B

#3 Lang

    Young Padawan

  • Members
  • Pip
  • 198 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 12 July 2009 - 07:08 PM

Well class A is a class that will handle all my queries. I have another class that handles all errors. If a query does not execute I log the mysql_error() string with the error class.

#4 dotbart

    Young Padawan

  • Members
  • Pip
  • 141 posts
  • Gender:Male
  • Location:Diepenbeek
  • Interests:Webdesign, Webdeveloppement, DJ, ...

Posted 13 July 2009 - 02:38 AM

Simple, use Singleton for your logger class
<?php
class Logger
{
  private static $instance;
  
  private function __construct()
  {
	  //Private constructor
  }
  
  public static function getInstance() {
	if(!isset(self::$instance)) {
	  self::$instance = new Logger();
	}
	return self::$instance;
  }
  public function logMysql($dbInstance)
  {
	  mysql_query("INSERT INTO LOGS (time,error,source),(" . time() . "," . mysql_error($connection). ",'MYSQL'",$dbInstance->connection);
  }
}
class Database
{
	private $connection;
	public function insert()
	{
		if(!mysql_query("INSERT SOMETHING WRONG"))
		{
			$this->onError();
		}
	}
	public function onError()
	{
		Logger::getInstance()->logMysql($this);
	}
}
?>

I don't know about using your Database class in your Error class to save your error, might get you into an infinite loop of mysql errors. In my opninion Logger classes are to be independent so the logging can't take control and stop the entire process.. But that's just my opinion :-)


Bart

#5 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 13 July 2009 - 05:51 PM

I gotta agree with dotbart. It would be better to restructure your code than to continue down the path you are going. In answer to your question though, no, PHP cannot redefine classes.

Extension or static methods are what you should look into.

#6 Lang

    Young Padawan

  • Members
  • Pip
  • 198 posts
  • Gender:Male
  • Location:Ontario, Canada

Posted 14 July 2009 - 11:55 PM

Thanks for the advice guys.

I went along with keeping the classes separate while I waited for an answer. Seems like that was a good idea.

Thanks again,
Lang





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users