Re: question about compositing objects

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi, Jonathan!

On Thu, 2006-01-12 at 14:13 -0800, jonathan wrote:
> I have a class which creates another class within it such as:
> 
> class Loc{
> 
> public function outputInfo()
> {
> 	
> 
> 	$map=new Map();
> 	
> 	$map->setKey();
> 	
> 
> }
> 
> }
> 
> In my main page can I access the $map object like this:
> 
> $loc=new Loc();
> 
> $loc->map->publicMapFunction();

Depending on what you're trying to accomplish, it looks like you're
running into a scope issue.  According to the code sample you provided,
the $map object is local to the Loc::outputInfo() method.  So, inside
this method, you can access the $map object like
$map->publicMapFunction() provided your Map class defines the
publicMapFunction() method.

If you want to access the $map object outside the outputInfo() method
defined in the Loc class, then you'll need to return the $map object or
provide some other interface to it within the Loc class.  

One way to do this would be something like this in the class definition:

class Loc{

public function outputInfo()
{
	$map = new Map();
	$map->setKey();
	return $map;
}

}

Next, in your main page you might do this:
$loc = new Loc();
$myMap = $loc->outputInfo();

Now, if you have the method publicMapFunction() defined in your Map
class, you should be able to access it like this:

$myMap->publicMapFunction();

I'm not sure the my example above is the best way to go about this...but
without knowing exactly what it is you're trying to accomplish I think
it at least should point you in the right direction.  

You'll also need to be aware of the difference between how PHP4 and PHP5
handle objects to know whether you're operating on a copy of an object
or a reference to the actual object itself.  

Hope that helps!

Tim 

-- 
Tim Boring
IT Manager
Automotive Distributors Warehouse
2981 Morse Road
Columbus, OH 43230
direct: 614-532-4240
toll free: 800-421-5556 x 4240
e-mail: tboring@xxxxxxxx

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux