Re: [PHP] PHP 5 Question about OO
On 2 Jun 2005, at 03:00, Richard Lynch wrote: Maybe I'm being dumb, but how can an object's __autoload function get called when the object class definition hasn't been loaded, and that's why you're calling __autoload in the first place... It wouldn't solve everything, but it would help. It solves the case where you have a class library that's quite heavily interconnected, and member functions instantiate other classes in that library (a common arrangement in my experience). By having an __autoload method, you get to at least partly avoid global handler clashes by having one that is specific to that library (it would work well in PEAR). If it is in a base class, then you are safe in the knowledge that any class can find any other in the same library, without tripping over anyone else's arrangements. You would still have to deal with the problem of finding the first class (which a global handler may help, subject to the issues that you've already raised), but once that's done, the problem is over. I don't see why they named it __autoload - the __ prefix is usually reserved for special purpose methods, not global functions. Just plain 'autoload' in a global context is not really anything wildly different in style to say ini_set or error_reporting. Marcus -- Marcus Bointon Synchromedia Limited: Putting you in the picture [EMAIL PROTECTED] | http://www.synchromedia.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP 5 Question about OO
On 2 Jun 2005, at 11:56, Colin Ross wrote: The way I see it, you are gonna be spending quite a bit of time writing all those lines of code for the class, what is bad about another requiring the file each time? Huh? Writing a 1-line function in a base class means that I would never have to write another require anywhere in my library (other to include the base class of course, which could conceivably be picked up by a global autoload anyway). Each of my 50-odd classes probably talks to 5 other classes in the library, giving a net saving of 250 lines. How is that bad? (I am talking hypothetically anyway as we don't have autoload methods at present). This thread is meant to be about how to improve automatic include file location - saying not to try to do it at all is not very helpful. Marcus -- Marcus Bointon Synchromedia Limited: Putting you in the picture [EMAIL PROTECTED] | http://www.synchromedia.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP 5 Question about OO
Richard Lynch wrote: On Wed, June 1, 2005 3:53 am, Marcus Bointon said: On 1 Jun 2005, at 11:38, Jochem Maas wrote: all true, now imagine that you install a couple of 3rdparty php5 'packages' and they all define __autoload() - ain't gonna work! which is why there has been discussion on internals regarding the possibility of use a handler- function stack for autoloading (in the same way that you can register a stack of input/output filter-function)... something to keep an eye on in case things change :-) I've run into this one. One way that would work for me (and initially it's how I assumed it worked) is for __autoload to be a standard class method like __construct, so that a class would attempt to run its own autoloader before breaking out to the global function namespace. Maybe I'm being dumb, but how can an object's __autoload function get called when the object class definition hasn't been loaded, and that's why you're calling __autoload in the first place... That seems like classic chicken/egg situation to me... that was my first reaction, but then I thought what if the __autoload() function was called when 'any' class needed to be included while running code inside said class... that _might_ actually be useful at any rate the __autoload() issue is still very much undecided :-) It's also incredibly likely that __autoload's being stacked won't work out too well. Consider this: foo software defines an __autoload Some foo_* classes get defined, autoloaded, everybody's happy. bar software defines an __autoload Some bar_* classes get defined, autoloaded, everybody's happy. Now some foo_* classes try to get instantiated, for whateve reason. bar's __autoload function is gonna kick, and that ain't good for foo_* Seems to me you'd need an array of regular expressions and the function to call: array('foo_*'='foo__autoload', 'bar_*'='bar__autoload') if you wanted to allow multiple __autoload functions to exist... I'm sure there are other ideas/solutions floating around, but that's A possibility. For performance, maybe just use stristr and 'foo_' instead of RegEx and 'foo_*' [shrug] -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP 5 Question about OO
Jochem Maas wrote: Richard Lynch wrote: On Wed, June 1, 2005 3:53 am, Marcus Bointon said: On 1 Jun 2005, at 11:38, Jochem Maas wrote: all true, now imagine that you install a couple of 3rdparty php5 'packages' and they all define __autoload() - ain't gonna work! which is why there has been discussion on internals regarding the possibility of use a handler- function stack for autoloading (in the same way that you can register a stack of input/output filter-function)... something to keep an eye on in case things change :-) I've run into this one. One way that would work for me (and initially it's how I assumed it worked) is for __autoload to be a standard class method like __construct, so that a class would attempt to run its own autoloader before breaking out to the global function namespace. Maybe I'm being dumb, but how can an object's __autoload function get called when the object class definition hasn't been loaded, and that's why you're calling __autoload in the first place... That seems like classic chicken/egg situation to me... that was my first reaction, but then I thought what if the __autoload() function was called when 'any' class needed to be included while running code inside said class... that _might_ actually be useful at any rate the __autoload() issue is still very much undecided :-) I opened up a feature request for this very topic a while back. The __autoload function should just register user-defined functions and store those func names in a stack so that (in turn) each function can require the appropriate file. If the first registered function fails to load the class then __autoload tries the next registered fucntion and so on until all of the registered functions have been tried. At this point if __autoload fails then we E_ERROR out explaining that __autoload could not load the class definition. As far as I can tell this is the cleanest solution that has been provided, but there is some disagreement over some of the details on this approach. just_kiddingLife would be so much easier if everyone just did things like the PEAR coders do/just_kidding -- Teach a man to fish... NEW? | http://www.catb.org/~esr/faqs/smart-questions.html STFA | http://marc.theaimsgroup.com/?l=php-generalw=2 STFM | http://php.net/manual/en/index.php STFW | http://www.google.com/search?q=php LAZY | http://mycroft.mozdev.org/download.html?name=PHPsubmitform=Find+search+plugins -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP 5 Question about OO
Jason Barnett wrote: Jochem Maas wrote: Richard Lynch wrote: On Wed, June 1, 2005 3:53 am, Marcus Bointon said: On 1 Jun 2005, at 11:38, Jochem Maas wrote: ... I opened up a feature request for this very topic a while back. The __autoload function should just register user-defined functions and store those func names in a stack so that (in turn) each function can require the appropriate file. If the first registered function fails to load the class then __autoload tries the next registered fucntion and so on until all of the registered functions have been tried. At this point if __autoload fails then we E_ERROR out explaining that __autoload could not load the class definition. As far as I can tell this is the cleanest solution that has been provided, but there is some disagreement over some of the details on this approach. agreed, but as you point out - the devil is in the details. :-) just_kiddingLife would be so much easier if everyone just did things like the PEAR coders do/just_kidding say-it-like-a-piratethat be fightin' talk m'friend ;-)/say-it-like-a-pirate -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP 5 Question about OO
Maybe it's just me, but the core concept of __autoload() seems to be broken to me. Moreover, every proposed solution I've heard about is totally inside the box. I would do it like this: 1) Define $_AUTOLOAD superglobal. 2) If I need SomeClass to be autoloaded I write this: $_AUTOLOAD['SomeClass'] = 'some/dir/some_classs.php'; 3) If I care, I could check whether $_AUTOLOAD['SomeClass'] is already set and generate an error. This would mimic Java behaviour (it's about competing with Java, is it?) without creating artificial headache for programmers. But wait! What do I know? This will cbreak BC; moreover, BC will be broken by this. Forget I said something. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP 5 Question about OO
On 6/2/05, GamblerZG [EMAIL PROTECTED] wrote: Maybe it's just me, but the core concept of __autoload() seems to be broken to me. Moreover, every proposed solution I've heard about is totally inside the box. I would do it like this: 1) Define $_AUTOLOAD superglobal. When forced to do OO, I use it like this: function __autoload( $class ) { require_once( $GLOBALS[LIB_PATH]/$class.class.php ); } -- Greg Donald Zend Certified Engineer http://destiney.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] PHP 5 Question about OO
Hi List, I'm using the oo orientation of PHP5 for a while now. All Classes I've got have a require on top them, if I try to reference to other classes. something like require (Class2.php); class Class1{ private function ... { $refClass2 = new Class2; } } Now my question, is it possible to skip that require/ include part? In Java you don't need that, as class and file names are identical. thx janbro -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP 5 Question about OO
On 1 Jun 2005, at 09:01, janbro wrote: require (Class2.php); class Class1{ private function ... { $refClass2 = new Class2; } } Now my question, is it possible to skip that require/ include part? In Java you don't need that, as class and file names are identical. PHP doesn't have this luxury - people can call files whatever they like. However, PHP5 does have a nice feature to deal with this. In your class1 class, create a function called __autoload like this: function __autoload($class_name) { require_once $class_name . '.php'; } In this case when you ask for a new class2 and you've not required it before, it will automatically call __autoload with $class_name set to 'Class2', which then requires the class file according to the pattern used in the function, in this case 'Class2.php'. Note that while PHP is not case sensitive to class names, the file system you're on probably is, so keep your case consistent throughout. Docs are here: http://www.php.net/manual/en/language.oop5.autoload.php Marcus -- Marcus Bointon Synchromedia Limited: Putting you in the picture [EMAIL PROTECTED] | http://www.synchromedia.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP 5 Question about OO
Hello janbro, Wednesday, June 1, 2005, 9:01:57 AM, you wrote: j Now my question, is it possible to skip that require/ include part? j In Java you don't need that, as class and file names are identical. Personally I'd __autoload them, but this might not be ideal for your file structure. Worth a look anyway perhaps? Best regards, Richard Davey -- http://www.launchcode.co.uk - PHP Development Services I do not fear computers. I fear the lack of them. - Isaac Asimov -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP 5 Question about OO
maybe have a look at extends class Class1 extends Class2 {... janbro wrote: Hi List, I'm using the oo orientation of PHP5 for a while now. All Classes I've got have a require on top them, if I try to reference to other classes. something like require (Class2.php); class Class1{ private function ... { $refClass2 = new Class2; } } Now my question, is it possible to skip that require/ include part? In Java you don't need that, as class and file names are identical. thx janbro -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP 5 Question about OO
Marcus Bointon wrote: On 1 Jun 2005, at 09:01, janbro wrote: require (Class2.php); I bet you didn't cut'n'paste that from a working file :-) class Class1{ private function ... { $refClass2 = new Class2; } } Now my question, is it possible to skip that require/ include part? In Java you don't need that, as class and file names are identical. PHP doesn't have this luxury - people can call files whatever they like. However, PHP5 does have a nice feature to deal with this. In your class1 class, create a function called __autoload like this: function __autoload($class_name) { require_once $class_name . '.php'; } In this case when you ask for a new class2 and you've not required it before, it will automatically call __autoload with $class_name set to 'Class2', which then requires the class file according to the pattern used in the function, in this case 'Class2.php'. Note that while PHP is not case sensitive to class names, the file system you're on probably is, so keep your case consistent throughout. all true, now imagine that you install a couple of 3rdparty php5 'packages' and they all define __autoload() - ain't gonna work! which is why there has been discussion on internals regarding the possibility of use a handler-function stack for autoloading (in the same way that you can register a stack of input/output filter-function)... something to keep an eye on in case things change :-) you may also consider that placing suitable require statements (I would use require_once for class files) before a class definition will probably/possibly (I don't know but it may be important to your project) be a minor performance boost over letting php call _autoload() whenever it needs. Docs are here: http://www.php.net/manual/en/language.oop5.autoload.php Marcus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP 5 Question about OO
eoghan wrote: maybe have a look at extends class Class1 extends Class2 {... he will still have to make sure that Class2 is loaded. besides wrapping 1 object inside another is perfectly acceptable, if my GOF terminology serves me well then this is usually termed a Delegation pattern (not that the name is that important, then again it helps to be talking about the same thing :-/). janbro wrote: Hi List, I'm using the oo orientation of PHP5 for a while now. All Classes I've got have a require on top them, if I try to reference to other classes. something like require (Class2.php); class Class1{ private function ... { $refClass2 = new Class2; } } Now my question, is it possible to skip that require/ include part? In Java you don't need that, as class and file names are identical. thx janbro -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP 5 Question about OO
On 1 Jun 2005, at 11:38, Jochem Maas wrote: all true, now imagine that you install a couple of 3rdparty php5 'packages' and they all define __autoload() - ain't gonna work! which is why there has been discussion on internals regarding the possibility of use a handler- function stack for autoloading (in the same way that you can register a stack of input/output filter-function)... something to keep an eye on in case things change :-) I've run into this one. One way that would work for me (and initially it's how I assumed it worked) is for __autoload to be a standard class method like __construct, so that a class would attempt to run its own autoloader before breaking out to the global function namespace. Marcus -- Marcus Bointon Synchromedia Limited: Putting you in the picture [EMAIL PROTECTED] | http://www.synchromedia.co.uk -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP 5 Question about OO
Okay, I see there is a workaround but nothing realy satisfying, but I'm not hoing to complain, I'm hapy to have oo in PHP at all. I never worked with __autoload so far, but I will give it a try and check it out thx janbro Marcus Bointon schrieb: On 1 Jun 2005, at 09:01, janbro wrote: require (Class2.php); class Class1{ private function ... { $refClass2 = new Class2; } } Now my question, is it possible to skip that require/ include part? In Java you don't need that, as class and file names are identical. PHP doesn't have this luxury - people can call files whatever they like. However, PHP5 does have a nice feature to deal with this. In your class1 class, create a function called __autoload like this: function __autoload($class_name) { require_once $class_name . '.php'; } In this case when you ask for a new class2 and you've not required it before, it will automatically call __autoload with $class_name set to 'Class2', which then requires the class file according to the pattern used in the function, in this case 'Class2.php'. Note that while PHP is not case sensitive to class names, the file system you're on probably is, so keep your case consistent throughout. Docs are here: http://www.php.net/manual/en/language.oop5.autoload.php Marcus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP 5 Question about OO
Alright thanks, I see there is a workaround to cope with that. I've never worked with __autoload, but will sure give it a look. I don't realy feel this satisfying, but hey I'm thankful to have oo at all. janbro Marcus Bointon schrieb: On 1 Jun 2005, at 09:01, janbro wrote: require (Class2.php); class Class1{ private function ... { $refClass2 = new Class2; } } Now my question, is it possible to skip that require/ include part? In Java you don't need that, as class and file names are identical. PHP doesn't have this luxury - people can call files whatever they like. However, PHP5 does have a nice feature to deal with this. In your class1 class, create a function called __autoload like this: function __autoload($class_name) { require_once $class_name . '.php'; } In this case when you ask for a new class2 and you've not required it before, it will automatically call __autoload with $class_name set to 'Class2', which then requires the class file according to the pattern used in the function, in this case 'Class2.php'. Note that while PHP is not case sensitive to class names, the file system you're on probably is, so keep your case consistent throughout. Docs are here: http://www.php.net/manual/en/language.oop5.autoload.php Marcus -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP 5 Question about OO
* janbro [EMAIL PROTECTED] : Okay, I see there is a workaround but nothing realy satisfying, but I'm not hoing to complain, I'm hapy to have oo in PHP at all. I never worked with __autoload so far, but I will give it a try and check it out The best way to use autoload is with the auto_prepend_file configuration directive. By adding the __autoload() function to a file that is automatically prepended, you don't even have to think about it in your scripts. This kind of flexibility is nice -- you can have different prepend files for different sites or different areas of the same site -- so that you only have the funcationality accessible that you need right there. You can also define your own naming schema -- which, admittedly is a dubious advantage, but an advantage nonetheless. Marcus Bointon schrieb: On 1 Jun 2005, at 09:01, janbro wrote: require (Class2.php); class Class1{ private function ... { $refClass2 = new Class2; } } Now my question, is it possible to skip that require/ include part? In Java you don't need that, as class and file names are identical. PHP doesn't have this luxury - people can call files whatever they like. However, PHP5 does have a nice feature to deal with this. In your class1 class, create a function called __autoload like this: function __autoload($class_name) { require_once $class_name . '.php'; } In this case when you ask for a new class2 and you've not required it before, it will automatically call __autoload with $class_name set to 'Class2', which then requires the class file according to the pattern used in the function, in this case 'Class2.php'. Note that while PHP is not case sensitive to class names, the file system you're on probably is, so keep your case consistent throughout. Docs are here: http://www.php.net/manual/en/language.oop5.autoload.php -- Matthew Weier O'Phinney | WEBSITES: Webmaster and IT Specialist | http://www.garden.org National Gardening Association| http://www.kidsgardening.com 802-863-5251 x156 | http://nationalgardenmonth.org mailto:[EMAIL PROTECTED] | http://vermontbotanical.org -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] PHP 5 Question about OO
On Wed, June 1, 2005 3:53 am, Marcus Bointon said: On 1 Jun 2005, at 11:38, Jochem Maas wrote: all true, now imagine that you install a couple of 3rdparty php5 'packages' and they all define __autoload() - ain't gonna work! which is why there has been discussion on internals regarding the possibility of use a handler- function stack for autoloading (in the same way that you can register a stack of input/output filter-function)... something to keep an eye on in case things change :-) I've run into this one. One way that would work for me (and initially it's how I assumed it worked) is for __autoload to be a standard class method like __construct, so that a class would attempt to run its own autoloader before breaking out to the global function namespace. Maybe I'm being dumb, but how can an object's __autoload function get called when the object class definition hasn't been loaded, and that's why you're calling __autoload in the first place... That seems like classic chicken/egg situation to me... It's also incredibly likely that __autoload's being stacked won't work out too well. Consider this: foo software defines an __autoload Some foo_* classes get defined, autoloaded, everybody's happy. bar software defines an __autoload Some bar_* classes get defined, autoloaded, everybody's happy. Now some foo_* classes try to get instantiated, for whateve reason. bar's __autoload function is gonna kick, and that ain't good for foo_* Seems to me you'd need an array of regular expressions and the function to call: array('foo_*'='foo__autoload', 'bar_*'='bar__autoload') if you wanted to allow multiple __autoload functions to exist... I'm sure there are other ideas/solutions floating around, but that's A possibility. For performance, maybe just use stristr and 'foo_' instead of RegEx and 'foo_*' [shrug] -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php