note 75278 added to language.types.object
| From: | gabe at fijiwebdesign dot com | Date: | Tue, 22 May 2007 04:25:48 +0000 |
| Subject: | note 75278 added to language.types.object | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-126810@lists.php.net to get a copy of this message | ||
In response to sirbinam.
You cannot call a function or method before it exists. In your example, the global instance of
stdout is just being passed around to differnet references (pointers). It however exists in the
"dump" function scope via the global keyword.
The code below works fine and illustrates that "stdout" has been defined before its
instantiation.
<?php
class profiler{
function profiler(){
$this->starttime = microtime();
}
function dump(){
global $stdout;
$this->endtime = microtime();
$duration = $this->endtime - $this->starttime;
$stdout->write($duration);
}
}
class stdout{
function write($msg){
echo $msg;
}
}
$stdout =& new stdout();
$profiler =& new profiler();
$profiler->dump();
?>
All classes and functions declarations within a scope exist even before the php execution reaches
them. It does not matter if you have your classes defined on the first or last line, as long as they
are in the same scope as where they are called and are not in a conditional statement that has not
been evaluated yet.
----
Server IP: 69.147.83.197
Probable Submitter: 210.7.27.26
----
Manual Page -- http://www.php.net/manual/en/language.types.object.php
Edit -- https://master.php.net/note/edit/75278
Del: integrated -- https://master.php.net/note/delete/75278/integrated
Del: useless -- https://master.php.net/note/delete/75278/useless
Del: bad code -- https://master.php.net/note/delete/75278/bad+code
Del: spam -- https://master.php.net/note/delete/75278/spam
Del: non-english -- https://master.php.net/note/delete/75278/non-english
Del: in docs -- https://master.php.net/note/delete/75278/in+docs
Del: other reasons-- https://master.php.net/note/delete/75278
Reject -- https://master.php.net/note/reject/75278
Search -- https://master.php.net/manage/user-notes.php