note 67930 added to language.oop5.autoload
| From: | alexey at renatasystems dot org | Date: | Thu, 06 Jul 2006 19:15:21 +0000 |
| Subject: | note 67930 added to language.oop5.autoload | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-114517@lists.php.net to get a copy of this message | ||
While using an "autoloading" method you should pay attention to variables scope. Because
of new file will be included INSIDE of magic function __autoload - all of declared in such file
global scope variables will be only available within this function and nowhere else. This will cause
strange behaviour in some cases. For example:
file bar.class.php:
<?php
$somedata = 'Some data'; /* global scope in common way */
class bar {
function __construct()
{
global $somedata; /* reference to global scope variable */
if ( isset($somedata) )
{
var_dump($somedata);
}
else
{
die('No data!');
}
}
}
?>
Attempt to load this file in common way:
<?php
require 'bar.class.php';
$foo = new bar();
?>
this will output (as expected):
string(9) "Some data"
But in case of __autoload:
<?php
function __autoload($classname)
{
require $classname . '.class.php';
}
$foo = new bar();
?>
you could expect that this script will return the same but no, it will return "No data!",
because defenition of $somedata after requiring treats as local within user-defined function
__autoload().
----
Server IP: 195.222.164.18
Probable Submitter: 85.21.236.46
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/language.oop5.autoload.php
Edit -- http://master.php.net/note/edit/67930
Del: integrated -- http://master.php.net/note/delete/67930/integrated
Del: useless -- http://master.php.net/note/delete/67930/useless
Del: bad code -- http://master.php.net/note/delete/67930/bad+code
Del: spam -- http://master.php.net/note/delete/67930/spam
Del: non-english -- http://master.php.net/note/delete/67930/non-english
Del: in docs -- http://master.php.net/note/delete/67930/in+docs
Del: other reasons-- http://master.php.net/note/delete/67930
Reject -- http://master.php.net/note/reject/67930
Search -- http://master.php.net/manage/user-notes.php