# Class names (with regard to autoload)

- Thread: https://externals.io/message/43191
- Original archive: https://news-web.php.net/php.internals/43191
- Started by Robin Burchell on 2009-02-26T20:27:36+00:00
- Messages: 6
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #43191 — Robin Burchell — 2009-02-26T20:27:36+00:00

URL: https://externals.io/message/43191#43191

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

---

## #43192 — Guilherme Blanco — 2009-02-26T20:48:40+00:00

URL: https://externals.io/message/43191#43192 · In reply to: #43191

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

---

## #43193 — Stuart — 2009-02-26T21:43:43+00:00

URL: https://externals.io/message/43191#43193 · In reply to: #43191

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

-- 
http://stut.net/

---

## #43194 — Robin Burchell — 2009-02-26T21:51:39+00:00

URL: https://externals.io/message/43191#43194 · In reply to: #43192

On Thu, Feb 26, 2009 at 8:48 PM, Guilherme Blanco
<guilhermeblanco@gmail.com> wrote:
> I doubt you'll find a patch to it.
>
> Mainly, the patch will be against this principle:
>
<snip>

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.

---

## #43195 — Lewis Wright — 2009-02-26T22:04:40+00:00

URL: https://externals.io/message/43191#43195 · In reply to: #43194

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>
>
> 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.

---

## #43211 — karpeles@ookoo.org — 2009-02-27T06:08:11+00:00

URL: https://externals.io/message/43191#43211 · In reply to: #43191

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
