note 35322 added to language.oop
| From: | jim at thestapletons dot org | Date: | Wed, 27 Aug 2003 16:26:02 +0000 |
| Subject: | note 35322 added to language.oop | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-55181@lists.php.net to get a copy of this message | ||
Here's something interesting I found that might be useful:
How to access parts of a class object used in another class:
class foo
{
var $a = "blah";
function blah()
{
echo "$a";
}
/*...*/
}
class bar
{
var $f = new foo();
function bad1()
{
$this->f->blah(); //look at the pretty parse/undefined variable error!
}
function bad2()
{
($this->f)->blah(); //another lovely error
}
function bad1()
{
{$this->f}->blah(); //more errors
}
function bad1()
{
$var = "f";
$this->$var->blah(); //you guessed it, error here.
}
function not_bad()
{
$f =& $this->f;
$f->blah; //echo: "blah": No error, I'm dissapointed...
}
/*...*/
}
So, if you want to call a function or access a variable of an encapsulated class, you should make a
reference to that specific class object in the function that uses it. If someone has a better idea
or method, please post, I'd appreciate it.
----
Manual Page -- http://www.php.net/manual/en/language.oop.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35322
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35322&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35322&report=yes