Re: question about compositing objects

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

 



On Thu, January 12, 2006 4:13 pm, 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();
>
> I thought I would be able to but it doesn't seem like it.

As others have noted, you probably want to initialize $loc->map to
$map inside the function.

However, you should also consider what happens if...

<?php
  //Assume classes are defined, but NOTHING else.
  $loc = new Loc();
  $loc->map->publicMapFunction();
?>

At this point, $loc->map has *** NO VALUE *** because you didn't call
outputInfo() yet.

So you will get a NOTICE (if you have E_ALL cranked up, as you should,
though it's not the default) and then an ERROR about calling a member
function on a non-object, and your whole project/script/page comes
crashing down around your ears.

You should *PROBABLY* design your code in such a way that this can
NEVER happen.

If this is a throw-away one-day hack, then ignore me.  If this code is
expected to live longer than a week, pay attention :-)

One solution is to initialize the ->map in the constructor, so that if
you get a valid Loc object, you have a valid map object.

/* PHP4 */
class Loc(){
  var $map = null;
  function Loc(){
    $this->map = new Map();
  }
}

/* PHP5 */
class Loc(){
  var $map = null;
  function __construct(){
    $this->map = new Map();
  }
}

/* Portable? for both 4&5 ??? */
class Loc(){
  var $map = null;
  function init(){
    $this->map = new Map();
  }

  function Loc(){ $this->init();}
  function __construct(){ $this->init();}
}

Disclaimer: I never use objects at all in PHP, so am not 100% certain
of this last example...

If you simply cannot make a valid Map() object in the constructor,
because there are other complex dependencies that cannot be resolved,
you should probably make the $loc->map attribute a PRIVATE variable,
and create a map() function to read that, so that you have to do:

$map = $loc->map();
$map->privateMapFunction();

The reason for the extra layer of the function is that you can include
error-checking in the Loc::map() function to:
1) Error out if $loc->map is NULL/invalid
2) Automagically create a Map() if $loc->map is NULL/invalid
3) Control the number of Map objects created, so you only have *ONE*
per Loc, and don't waste RAM by endlessly re-creating Map objects
.
.
.

The Loc::map() function will create a "check-point" for you to write
"gate-keeper" code to ensure that everything is kosher to to provide a
more robust body of code.

The down-side is, more code to maintain and an additional layer of
complexity for the end user.

So, if you CAN make a Map() in the Loc constructor, that's
easier/simpler.

If not, a public Map function for a private attribute is clean.

Just tossing Map objects into the map attribute willy-nilly is fine
for quickie hacks, but little else.

The fact that there is a *TON* of badly-written code out there, all
over the 'net, and in steady heavy use today, that does exactly what
I'm telling you not to do, does not invalidate this post. :-)

-- 
Like Music?
http://l-i-e.com/artists.htm

-- 
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