Hi Hannes. Ok, so I was wrong about the cause of the "symptom", good - so this is instead another problem: No autoloading of functions... I tried a similar example, where you switch the order of "new foo()" and "bar()" around, and it fails, as it doesn't find the definition of bar(), since class.php hasn't been included at this point...
Ok, new question: Autoloading of functions... Has it been considered? Any fundamental problems with it? (Again, except for others requesting the same, I've not found anything else about it, so pointers to any previous discussion of this would be appreciated) Remember that in my example, I called a "factory function" to get an instance of the class, and as you show, it's possible to get that - _after_ you've instantiated the class some other way (or have it call __autoload() in some way), but in the first call to that function, you typically haven't instantiated the class, before... Regards, Terje ----- Original Message ----- From: "Hannes Magnusson" <[EMAIL PROTECTED]> To: "Terje Slettebø" <[EMAIL PROTECTED]> Cc: <internals@lists.php.net> Sent: Sunday, September 10, 2006 4:44 PM Subject: Re: __autoloading and functions Hello Terje What are you talking about? --class.php-- <?php class foo { } function bar() { print "Hello World\n"; } --foo.php-- <?php function __autoload($class) { include "class.php"; } new foo(); bar(); print "hello world";... -Hannes On 9/10/06, Terje Slettebø <[EMAIL PROTECTED]> wrote: > Hi all. > > I don't know if this has been discussed before (I've not found it from doing > a search), but if it has, please provide me with a link to the discussion. > > __autoload() is very convenient, but it has one problem: While classes > defined in the __autoload() function (via an include) are accessible > globally, any functions being included are not accessible outside the > __autoload() function, making them completely inaccessible to the rest of > the system. This means that if you have a file containing a class, as well > as one or more associated functions, you won't be able to use the functions, > if the file containing the class and functions is loaded using > autoloading... > > I've not found a workaround for this (except reintroducing > include_once/require_once, which defeats the whole purpose of > autoloading...), are others also experiencing problems with this, and if > not, do you a) not use any functions, or b) manage some other way? > > Has there been considerations for solving this in some way? > > Example: > > --- email_address.php --- > > class EmailAddress > { > public function __construct($address) { ... } // Check if string contains > a valid email address > .... > private $address; > } > > function email_address($address) > { > return new EmailAddress($address); > } > > --------------------------------- > > You may then use this like: > > $address=email_address(<some string expression>); > > to make the conversion to EmailAddress less "obtrusive" (simulating implicit > conversion to user-defined type). > > However, this doesn't work with autoloading, so you have to either manually > include the above file, or use "new" directly: > > $address=new EmailAddress(<some string expression>); > > In other words, this function is not a candidate for making it a method. > > The problem in this particular example would go away if there was a way to > implicitly convert from fundamental types to user-defined types, but that's > another discussion... > > Regards, > > Terje -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: http://www.php.net/unsub.php