Static vars & objects reference

From: Date: Thu, 20 May 2004 10:15:39 +0000
Subject: Static vars & objects reference
Groups: php.pear.general 
Request: Send a blank email to pear-general+get-12648@lists.php.net to get a copy of this message
Hi All, I'm doing a project all based on PEAR libraries, when trying to implement a singleton pattern I found no expected results. The the following source coded should output "new.log" in filename at the last var_dump but it doesn't. ==== CUT HERE==== <?php require_once 'PEAR.php'; function check($classname, $obj) { echo str_repeat('=', 30) . $classname . str_repeat('=', 30) . "\n"; echo "is_object: " . is_object($obj) . "\n"; echo "isset: " . isset($obj) . "\n"; echo "is_array: " . is_array($obj) . "\n"; echo "empty: " . empty($obj) . "\n"; echo "is_resource: " . is_resource($obj) . "\n"; echo "is_a(PEAR): " . is_a($obj, 'PEAR') . "\n"; echo str_repeat('=', 60 + strlen($classname)) . "\n"; } class Logger extends PEAR { var $filename; function Logger($filename) { $this->set_file($filename); } function log($line, $file, $msg, $class = null) { $fp = fopen($this->filename, 'a+'); if (!$fp) { return $this->raiseError( "Can't open: " . $this->filename); } fwrite($fp, sprintf("%s(%d):%s:%s", $file, $line, $msg, is_object($class) ? get_class($class) : "-")); fclose($fp); } function set_file($filename) { $this->filename = $filename; } } class Application extends PEAR { var $logFiles; var $DSNs; function &the_app() { static $app; check('Application', $app); if (!isset($app)){ echo "Building the Application\n"; $app =& new Application(); $app->init(); } return $app; } function init() { $this->logFiles = array(); $this->DSNs = array(); // Should get from a Conf file. $this->logFiles['default'] = './old.log'; $this->DSNs['default'] = "mysql://root:passwd@localhost/tests"; } function &get_log($which = 'default') { static $logs; if (!isset($logs)) $logs = array(); check('Logger', $logs[$which]); if (!isset($logs[$which])) { echo "Building the log '$which'\n"; $logs[$which] =& new Logger($this->logFiles[$which]); if (PEAR::isError($logs[$which])) { die($logs[$which]->toString()); } } return $logs[$which]; } function &get_db($which = 'default') { static $dbs = array(); if (!isset($dbs)) $dbs = array(); check('DB', $dbs[$which]); if (!isset($dbs[$which])) { echo "Building the DB '$which'\n"; $dbs[$which] =& DB::connect($this->DSNs[$which]); if (PEAR::isError($dbs[$which])) { die($dbs[$which]->toString()); } } return $dbs[$which]; } } ####################################################################3 $app =& Application::the_app(); $log =& $app->get_log(); print_r($log); $log->set_file('new.log'); print_r($log); $appp =& Application::the_app(); #print_r($appp); $logg =& $app->get_log(); print_r($logg); ?> ==== CUT HERE==== Why is $theApp built ever time? Can anyone tell what I'm missing? All test are done with php 4.3.2 and apache 1.3.20? at the php.ini the "allow_call_time_pass_reference" is setted to "On", besides it's been said that has no effect. Regards, Virgilio Sanz ============================================================================ This e-mail message and any attached files are intended SOLELY for the addressee/s identified herein. It may contain CONFIDENTIAL and/or LEGALLY PRIVILEGED information and may not necessarily represent the opinion of this company. If you receive this message in ERROR, please immediately notify the sender and DELETE it since you ARE NOT AUTHORIZED to use, disclose, distribute, print or copy all or part of the contained information. Thank you. ============================================================================

« previous php.pear.general (#12648) next »