Re: Class dependencies

From: Date: Sat, 11 Sep 2004 18:59:10 +0000
Subject: Re: Class dependencies
Groups: php.general 
Request: Send a blank email to php-general+get-196407@lists.php.net to get a copy of this message
* Thus wrote Ed Lazor: > I'm creating classes that rely on the presence of other classes that I've > created. Does PHP5 provide a way to specify these dependencies or a way to > check these dependencies when the class is instantiated? For example, class > Automobile would fail to instantiate if class Tire wasn't available. > > I know the __autoload feature of PHP5 could be used to flag an error like > this: > > function __autoload($class_name) { > if (!@include("$class_name.php")) { > echo "Error: Unable to load $class_name.<br>"; > } > } What you can do is something like this: function __autoload($class_name) { static $_stack = array(); // push the calls of autoload here array_push($_stack, $class_name); if ( !@include("$class_name.php") || !class_exists($class_name, false) ) { var_dump($_stack); $_stack = array(); // reset it echo "Error: Unable to load $class_name.<br>"; return false; } // pop the stack array_pop($_stack); return true; } Curt -- The above comments may offend you. flame at will.

« previous php.general (#196407) next »