note 68614 deleted from language.oop5.autoload by crell

From: Date: Mon, 04 Apr 2022 16:08:03 +0000
Subject: note 68614 deleted from language.oop5.autoload by crell
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-218019@lists.php.net to get a copy of this message
Note Submitter: gonix ---- in response to alexey at renatasystems dot org: You may add `global $somedata; before $somedata = 'Some data';` and it should work as expected. file bar.class.php: <?php global $somedata; $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!'); } } } ?> 'common way': <?php require 'bar.class.php'; $foo = new bar(); ?> '__autoload way': <?php function __autoload($classname) { require $classname . '.class.php'; } $foo = new bar(); ?> Both 'comon way' and '__autoload way' should give same result: string(9) "Some data"

« previous php.notes (#218019) next »