note 46290 added to language.oop5
| From: | farzanatifarzandotcom at osu1 dot php dot net | Date: | Tue, 05 Oct 2004 23:04:09 +0000 |
| Subject: | note 46290 added to language.oop5 | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-78046@lists.php.net to get a copy of this message | ||
PHP 5 is very very flexible in accessing member variables and member functions. These access methods
maybe look unusual and unnecessary at first glance; but they are very useful sometimes; specially
when you work with SimpleXML classes and objects. I have posted a similar comment in SimpleXML
function reference section, but this one is more comprehensive.
I use the following class as reference for all examples:
<?
class Foo {
public $aMemberVar = 'aMemberVar Member Variable';
public $aFuncName = 'aMemberFunc';
function aMemberFunc() {
print 'Inside
aMemberFunc()';
}
}
$foo = new Foo;
?>
You can access member variables in an object using another variable as name:
<?
$element = 'aMemberVar';
print $foo->$element; // prints "aMemberVar Member Variable"
?>
or use functions:
<?
function getVarName()
{ return 'aMemberVar'; }
print $foo->{getVarName()}; // prints "aMemberVar Member Variable"
?>
Important Note: You must surround function name with { and } or PHP would think you are calling a
member function of object "foo".
you can use a constant or literal as well:
<?
define(MY_CONSTANT, 'aMemberVar');
print $foo->{MY_CONSTANT}; // Prints "aMemberVar Member Variable"
print $foo->{'aMemberVar'}; // Prints "aMemberVar Member Variable"
?>
You can use members of other objects as well:
<?
print $foo->{$otherObj->var};
print $foo->{$otherObj->func()};
?>
You can use mathods above to access member functions as well:
<?
print $foo->{'aMemberFunc'}(); // Prints "Inside aMemberFunc()"
print $foo->{$foo->aFuncName}(); // Prints "Inside aMemberFunc()"
?>
----
Manual Page -- http://www.php.net/manual/en/language.oop5.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+46290
Delete -- http://master.php.net/manage/user-notes.php?action=delete+46290&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+46290&report=yes
Search -- http://master.php.net/manage/user-notes.php