From: Randall H. W. <rh...@ea...> - 2002-05-26 14:15:54
|
> Hello everyone! I need some helpful advice if anyone has some for me :) > > I'm trying to get include statements to work inside a class definition but I > keep getting errors everytime I try it. Here is a basic idea of what I'm > doing: > class foo > { > var $temp; > > include("class_functions.php"); > } > > As I stated before, this syntax results in an error. If anyone has an idea > of how I could get this to work please let me know. > > Thanks! > Adam Adam: In-so-far as I can tell, that just won't work. To get around it, you will have to either: 1) Write all your class functions within the class (simply edit "class_functions.php", wrapping all the code in that file that is NOT global within the class defining syntax), or 2) Write stub functions in the class for each function in class_functions.php. This is probably a better method if you are porting code, as you will be able to replace the stub functions with working code without breaking the old code in the process. So why won't what you're doing work? Every single included or required class is parsed in the GLOBAL namespace, not within your class. ($this-> references will not work if within functions in class_functions.php). A class is a data construct, most likely stored within memory as a complex associative array (this is basically how PERL, Java, C++, Objective-C, Visual Basic all do it--I don't think PHP is going to be much different that C++ in this regard). The functions are basically anonymous functions within the class, and PHP knows how to handle the class data types. So including a file in the middle of the definition is not going to fly with PHP. Sorry I can't help, but I hope this helps explain why. -- Randall Wood rh...@ma... |