note 47423 added to language.types.object

From: Date: Tue, 16 Nov 2004 02:46:05 +0000
Subject: note 47423 added to language.types.object
Groups: php.notes 
Request: Send a blank email to php-notes+get-80573@lists.php.net to get a copy of this message
<?php class hack{} $hack =& new hack; class profiler{ function profiler(){ $this->startime = microtime(); } function dump(){ global $hack; $this->endtime = microtime(); $duration = $this->endtime - $this->starttime; /* this won't actually work, it just the concept */ $stdout->write($duration); } } class stdout{ function write($msg){ echo $msg; } } $stdout =& new stdout(); $hack =& $stdout; $profiler->dump(); ?> /* * * In short this little hack allows us to call $stdout->write() from * $profiler->dump * * The problem is that $stdout doesn't exist yet and when the compiler * parses this class, it sends a fatal error and dies because you can't * refer to a method of an object that doesn't exist yet, even though * this method doesn't get called until the end of execution, when the * method does exist. * This is the same as not being able to use a function before it is * at least declared in say a header file. This is seen in C, Perl, * and pretty much every language known to man. (TTBOMK?) * * So what does this hack do? * The first thing that happens in the global scope is an empty class * definition, it then creates a object called $hack from this class. * All this does is allocate memory for an object, and places a pointer * at the begining of that memory segment. * When the compiler parses this class, it doesn't care that the $hack * object is empty, as long as it has somewhere to assign a function * pointer. Later in global scope the $stdout object is created. * After $stdout is created, we do $hack =& $stdout. The =& * (assign by reference) moves the pointer for $hack to the begining of * the memory segment for $stdout. So when we call $hack->write(), it * points to the exact same object->method() as $stdout->write(). So * this is actually very reliable, just don't tell a purist! */ ---- Manual Page -- http://www.php.net/manual/en/language.types.object.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+47423 Delete -- http://master.php.net/manage/user-notes.php?action=delete+47423&report=yes Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47423&report=yes&reason=bad+code Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47423&report=yes&reason=spam Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47423&report=yes&reason=useless+example Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47423&report=yes&reason=contains+commercial+links Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47423&report=yes&reason=useless+note Reject -- http://master.php.net/manage/user-notes.php?action=reject+47423&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#80573) next »