Hi all,
I've been playing around with autoload lately, and specifically using
autoload to load classes which may be in subdirectories. I came up
with the following test script:
<?php
function __autoload($sName)
{
echo "autoloading " . $sName . "\n";
}
$s = "Foo.Bar";
$a = new $s();
$b = new Foo.Bar();
?>
The first line demonstrates what I'm after ("autoloading Foo.Bar"),
and the second seems to give a bit of an odd/incorrect (in these
circumstances) result(__autoload is invoked with "Foo").
Is it possible to reconcile this behaviour to be a bit more
consistant, and provide the behaviour I'm after?
I'm willing to try provide a patch, if someone would be kind enough to
direct me to the part(s) of the engine I'd need to look at.
Thanks,
Robin
I doubt you'll find a patch to it.
Mainly, the patch will be against this principle:
<?php
define('Part', 'Foo');
define('SubPart', 'Bar');
class FooBar {
// ...
}
$fb = new Part.SubPart();
Regards,
Hi all,
I've been playing around with autoload lately, and specifically using
autoload to load classes which may be in subdirectories. I came up
with the following test script:<?php
function __autoload($sName)
{
echo "autoloading " . $sName . "\n";
}$s = "Foo.Bar";
$a = new $s();
$b = new Foo.Bar();
?>The first line demonstrates what I'm after ("autoloading Foo.Bar"),
and the second seems to give a bit of an odd/incorrect (in these
circumstances) result(__autoload is invoked with "Foo").Is it possible to reconcile this behaviour to be a bit more
consistant, and provide the behaviour I'm after?I'm willing to try provide a patch, if someone would be kind enough to
direct me to the part(s) of the engine I'd need to look at.Thanks,
Robin--
--
Guilherme Blanco - Web Developer
CBC - Certified Bindows Consultant
Cell Phone: +55 (16) 9215-8480
MSN: guilhermeblanco@hotmail.com
URL: http://blog.bisna.com
São Paulo - SP/Brazil
On Thu, Feb 26, 2009 at 8:48 PM, Guilherme Blanco
guilhermeblanco@gmail.com wrote:
<snip>I doubt you'll find a patch to it.
Mainly, the patch will be against this principle:
Hmm, that wouldn't actually have an impact, as (at least, what I'm
aiming for/wanting to do) is not to introduce periods as valid in
actual class names, but to allow them to be used for autoloading - so
I can have something like this (abbreviated terribly, apologies):
function __autoload($sFoo)
{
include(str_replace(".", "/", $sFoo));
}
.. so, your example would expand to Foo.Bar, which given that periods
aren't allowed in a class name doesn't exist as a class, and won't
work. Unless I'm missing something?
Actually, I don't believe that expanding is even done - i.e. it
doesn't respect defines there
Seems that way on testing, too:
class FooBar
{
// ...
}
define('Part', 'Foo');
define('SubPart', 'Bar');
$fb = new Part.SubPart();
gives: Fatal error: Class 'Part' not found
.. but that part of things is a going off on a bit of a tangent.
I think what you're doing is effectively the same as doing:
$fb = new Part .''. SubPart();
So basically concatenating a new instance of Part, with the results
from the function SubPart. Try defining Part as a class and see what
error you get. If I'm right, you'll get an undefined function error.
2009/2/26 Robin Burchell viroteck@viroteck.net:
On Thu, Feb 26, 2009 at 8:48 PM, Guilherme Blanco
guilhermeblanco@gmail.com wrote:<snip>I doubt you'll find a patch to it.
Mainly, the patch will be against this principle:
Hmm, that wouldn't actually have an impact, as (at least, what I'm
aiming for/wanting to do) is not to introduce periods as valid in
actual class names, but to allow them to be used for autoloading - so
I can have something like this (abbreviated terribly, apologies):function __autoload($sFoo)
{
include(str_replace(".", "/", $sFoo));
}.. so, your example would expand to Foo.Bar, which given that periods
aren't allowed in a class name doesn't exist as a class, and won't
work. Unless I'm missing something?Actually, I don't believe that expanding is even done - i.e. it
doesn't respect defines thereSeems that way on testing, too:
class FooBar
{
// ...
}define('Part', 'Foo');
define('SubPart', 'Bar');$fb = new Part.SubPart();
gives: Fatal error: Class 'Part' not found
.. but that part of things is a going off on a bit of a tangent.
2009/2/26 Robin Burchell viroteck@viroteck.net
Hi all,
I've been playing around with autoload lately, and specifically using
autoload to load classes which may be in subdirectories. I came up
with the following test script:<?php
function __autoload($sName)
{
echo "autoloading " . $sName . "\n";
}$s = "Foo.Bar";
$a = new $s();
$b = new Foo.Bar();
?>The first line demonstrates what I'm after ("autoloading Foo.Bar"),
and the second seems to give a bit of an odd/incorrect (in these
circumstances) result(__autoload is invoked with "Foo").Is it possible to reconcile this behaviour to be a bit more
consistant, and provide the behaviour I'm after?I'm willing to try provide a patch, if someone would be kind enough to
direct me to the part(s) of the engine I'd need to look at.
The concatenation operator (.) is not valid in a class name. Use an
underscore and you'll be able to get the behaviour you want.
-Stuart
Le jeudi 26 février 2009 à 20:27 +0000, Robin Burchell a écrit :
I've been playing around with autoload lately, and specifically using
autoload to load classes which may be in subdirectories. I came up
with the following test script:
The best solution for you is to wait for PHP 5.3.0, then use:
function __autoload($class) {
$path = CLASSES_DIR . '/' . str_replace($class, '\', '/', $class) .
'.class.php';
include($path);
}
Namespaces are probably the best way to achieve this (apart from using
_).
Mark