You are here: Re: [PHP] question about compositing objects « PHP « IT news, forums, messages
Re: [PHP] question about compositing objects

Posted by Tim Boring on 01/13/06 01:01

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@adw1.com

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация