Page 2 of 2

Posted: Fri Dec 23, 2005 10:56 am
by John Cartwright
In other words, you're operating under the assumption that you must first populate the registry with the classes you would need before using them? Is my understanding of what you're saying here correct?
I believe the registry should be able to detect if the object exists, and if not create it..

Posted: Fri Dec 23, 2005 11:00 am
by BDKR
Jcart wrote:In other words, you're operating under the assumption that you must first populate the registry with the classes you would need before using them? Is my understanding of what you're saying here correct?

I messed that statement up. I agree with him (he's correct) that there isn't much point. If you read what I posted beyond that, it should be clear that I agree with you about ...
Jcart wrote:I believe the registry should be able to detect if the object exists, and if not create it..

Posted: Fri Dec 23, 2005 12:40 pm
by dbevfat
Jcart wrote:I believe the registry should be able to detect if the object exists, and if not create it..
If it creates objects, it becomes a ServiceLocator, doesn't it?

Posted: Fri Dec 30, 2005 4:01 am
by dottedquad
BDKR wrote:Interaction between classes is called message passing. For one object to pass a message to another, it has to be aware that it's there to take the message. Here is an example.

Code: Select all

class db
	{
	var $error;
	
	function db(&$error)
		{ $this->error=&$error; }
	
	function queryDB($SQL)
		{
		/* act as if query failed */
		$this->error->testError('The query failed!');
		return false;
		}
	}
	
	
class news
	{
	var $error;
	var $db;

	function news(&$db, &$error)
		{
		$this->db=&$db;
		$this->error=&$error;	
		}
	
	function getNews($top_subject, $from_date, $to_date)
		{
		if($this->db->queryDB($SQL)==false)
			{ $this->error->testError('News retrieval failed!'); }
		return false;
		}
	}
	
	
class error
	{
	
	function testError($err_str)
		{ echo "This output is from the error object!\n".$err_str."\n"; }
	
	}
	

# Instantiate objects
$err=new error;
$db=new db($err);
$news=new news($db, $err);

# Try to get the news
$news->getNews('weather', '1/2005', '3/2005');
Both the news and db objects have a handle to the same error object. How I did this is obvious, but take note of the fact in the constructor declerations that the objects are passed in by reference!

When you run this, you will actually see two messages. In other words, not only is the $news object sending a message to the error object, but the db object is as well. From one request, we were able to start a chain of events that was the message passing amongst multiple objects. As you can see, a message is initially passed from the $news object to the $db object.

Hope that helps,
Cheers
I'm trying to do the same exact thing: viewtopic.php?t=42365
Am I wrong but, is testError in $this->error->testError('The query failed!'); a function? I'm trying to make sence of this class stuff.

Posted: Fri Dec 30, 2005 8:10 pm
by mickd
dottedquad wrote: I'm trying to do the same exact thing: viewtopic.php?t=42365
Am I wrong but, is testError in $this->error->testError('The query failed!'); a function? I'm trying to make sence of this class stuff.
its a method of a class of a variable of a class. so take this example

Code: Select all

class errorhandling {
     private $foo;
     public function construct() {
     }
     public function destruct() {
     }
     public function testError($msg) {
     die($msg);
     }
}

class news {
     private $bar;
     public $error;
     public function construct(&$error) {
     $this->error=&$error;
     }
     public function destruct() {
     }
     public function MakeError() {
     $this->error->testError('Make This Hurt');
     }
}

$error = new error;
$news = new news($error);
$news->MakeError();
//this will produce
Make This Hurt
hope that makes sense.

EDIT: since $this->error in class news was assigned an object(errorhandling), to access that objects(errorhandling) methods you use $this->error->ErrorsMethod(). similarly you can call the error objects(errorhandling) varaibles etc.

Posted: Wed Jan 04, 2006 3:55 am
by jurriemcflurrie
Hey I'm back again. We moved with the business i work so i couldn't respond as there was no internet.
Jcart wrote:I believe the registry should be able to detect if the object exists, and if not create it..
I understand that's the point, but how do i create a object wich doesn't exists? :P

I wonder if there's an other tutor then you suggested, couse it doesn't help me much with further usage of the theory.

I'm very interested in this whole thing but can't find much about it (as usual :) )

Posted: Wed Jan 04, 2006 7:22 am
by mickd
something like this?

Code: Select all

class foo {
   private $bar;
   
   public function getBar() {
      if(!is_object($this->bar)) {
      $this->bar = new otherClass;
      }
   return $this->bar;
   }
}
not sure if this is how they do it...

Posted: Wed Jan 04, 2006 7:55 am
by jurriemcflurrie
That's not what I meant :) I know how to code classes but the problem is those registries. I have to feed them don't i?

My theory for now:
- Feed reg with classes
- Access it with $reg->entry->method();
- So I can do $this->otherentry->othermethod(); from somewhere within the registry.

So where do I get those entries from? I still have to include them? And then $reg->addentry(); them?

Posted: Wed Jan 04, 2006 8:07 am
by mickd
yes, unless theres something wacked up going on that im not catching on to (apparently its happened before 8O) ;)

Posted: Wed Jan 04, 2006 11:27 am
by John Cartwright
jurriemcflurrie wrote:Hey I'm back again. We moved with the business i work so i couldn't respond as there was no internet.
Jcart wrote:I believe the registry should be able to detect if the object exists, and if not create it..
I understand that's the point, but how do i create a object wich doesn't exists? :P

Sorry I don't really follow you on this one? What do you mean doesn't exist?>

Posted: Wed Jan 04, 2006 11:35 am
by jurriemcflurrie
Well it checks if it exists you said. And else it will create one. But I cant write php that create a whole object :P




I don't follow it completely neither. Can anyone post an 'complete' example of a registry? I would really apriciate it :)

Posted: Wed Jan 04, 2006 12:02 pm
by John Cartwright

Posted: Thu Jan 05, 2006 4:00 am
by jurriemcflurrie
Complete? I don't see him using it.

Edit
It's hard for me to explain the problem in english but I will give it a try again.

I really like to know how to use the registry class. How do I use methods from somewhere in de registry from a db class for instance? What is the proper way to add a class into the registry?

I hope I'm clear now and that I'm not annoying :) If someone could post some practical examples it would help me a lot i think.

Posted: Thu Jan 05, 2006 1:40 pm
by BDKR
jurriemcflurrie wrote: I really like to know how to use the registry class. How do I use methods from somewhere in de registry from a db class for instance? What is the proper way to add a class into the registry?

I hope I'm clear now and that I'm not annoying :) If someone could post some practical examples it would help me a lot i think.
I have a method in my factory class that returns a handle to an object of the type I'm after. That said, my factory / registry class is aware at instantiation of all the classes I may ask for. Essentially, I have a method in the factory called returnObject($class, $new='n'). The internals of this method will have (among others things) a switch that takes the value of class and instantiates the corresponding object. I store the object in a private array that is an element of the factory class and return a reference to it.

Now the list of classes that my factory class is aware of (and thus able to return) at runtime is hard coded (but it's part of a db abstraction library that I've been working on for some time), but I'm sure I could expand that factory to be capable of instantiating objects that were not known until runtime via a hash table that's either pre-existing or generated at instantiation by exploring a directory (or directories, which could be real slow if not done carefully), or a config file.

There are more potential possibilities here, but i'm going to cut it short for now.

Cheers