> -----Original Message-----
> What you can do is something like this:
> 
> 
1> function __autoload($class_name) {
2>   static $_stack = array();
3> 
4>   // push the calls of autoload here
5>   array_push($_stack, $class_name);
6> 
7>      if ( [EMAIL PROTECTED]("$class_name.php") ||
8>        !class_exists($class_name, false) ) {
9> 
10>     var_dump($_stack);
11>     $_stack = array(); // reset it
12>     echo "Error:  Unable to load $class_name.<br>";
13> 
14>     return false;
15>   }
16> 
17>   // pop the stack
18>   array_pop($_stack);
19> 
20>   return true;
21> 
22> }

Hi Curt =)

This didn't seem to work.  The stack stuff just ends up dumping the value of
$class_name.  Also, why check to see if the class exists on line #8?  Isn't
PHP calling __autoload already confirming that the class doesn't exist?

I'll include copies of the two scripts below.  Here's information on the
scripts that might help explain things.

The first script is being saved as Test.php.  It includes a class called
Test. 

The second script is being saved as test2.php.  This script has a class
called ClassDependent.  It depends on the Test class.

I'm trying to address two situations are being addressed here.  What if the
file Test.php is missing for some reason.  And, even if the file is present,
what happens if the file / class hasn't been loaded?

In my opinion, if class Test is missing, it makes sense to know up front.
It provides a sort of checks and balances.  Otherwise, the class doesn't
show as missing until there's an attempt to load it.  I can easily see
deploying an app without realizing there's a problem until someone happens
to use some osbure feature that depends on the missing class.

Maybe I'm being paranoid (opinions? - be nice :P hehe), but once reason I'm
looking into using OOP is to reduce bugs and I'm just looking at possible
pitfalls.  It would be nice to hear how you guys handle this with PHP-based
apps that you deploy.

I know that I could just create a list of files belonging to the app and
check to make sure that they are all present when starting.  It seems like
there could be a lot of overhead in maintaining something like this.  It
also seems to make sense that you'd want classes to define their own
dependencies for portability.  What do you think?

All of this kind of leads into why I'd like autoload (or some other tool) to
indicate which class ran into a problem loading another class.  In the
examples below, it helps troubleshoot things if the error message tells me
that ClassDependent was unable to load class Test.  Hehe Having the error
message tell me which line number would also be super cool, but maybe that's
not possible.

Anyway, I look forward to hearing what you guys think.

Thanks,

Ed


Scripts:


---------------------------------
Test.php
---------------------------------

<?php

class Test {
        public $value1 = 0;
        
        function set_value1($data) {
                $this->value1 = $data;
        }
        
        function get_value1() {
                return $this->value1;
        }
}

?>

---------------------------------
Test2.php
---------------------------------

<?php

function __autoload($class_name) {
  static $_stack = array();

  // push the calls of autoload here
  array_push($_stack, $class_name);

        if ( [EMAIL PROTECTED]("$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;

}

class ClassDependent {
        private $MyObject;
        
        function ClassDependent() {
                $this->MyObject = new Test;
        }

        function get_name() {
                return $this->MyObject->get_value1();
        }
        
        function set_name($data) {
                return $this->MyObject->set_value1($data);
        }
}

$temp = new ClassDependent;
$temp->set_name("Ed");
print "This is a test...  " . $temp->get_name() . "<br>";

?>

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to