Welcome! Log In Create A New Profile

Advanced

[PHP] redeclare classes dynamically

Posted by viraj 
viraj
[PHP] redeclare classes dynamically
October 22, 2008 11:25AM
i'm trying to dynamically initialize bunch of classes from different
paths but with the same class and file names.

while ($register->request->m != 'exit') {
$controlFile = './core/' . $register->request->m . '/controller.class.php';

if (is_file($controlFile)) {
require_once ($controlFile);
$controller = new controller($register);
}
}

the while loop should continue till one of those classes set 'exit' as
the $register->request->m value, where it exists loop and reach the
eos.

but i get this fatal error.. Cannot redeclare class controller in
/home/vir blah blah..

yeah, i know it's because i try to declare the same class name several times.

my question is.. what is the best way to implement this?

many thanks

~viraj

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Martijn Korse
[PHP] Re: re[PHP] declare classes dynamically
October 22, 2008 03:35PM
Defining multiple classes with the same name but different implementation
doesn't seem like a good idea to me. Why did you choose to do this?

also, have a look at this function:
http://www.php.net/autoload

i'm not sure what you're trying to accomplish exactly, but whatever it is,
it might be helpfull :-)

-----
http://devshed.excudo.net http://devshed.excudo.net
--
View this message in context: http://www.nabble.com/redeclare-classes-dynamically-tp20106931p20109780.html
Sent from the PHP - General mailing list archive at Nabble.com.


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
uaca man
Re: [PHP] redeclare classes dynamically
October 22, 2008 05:35PM
From your code it looks like $controller will have only the last instance to
the controller class, so you can only load the last request and instantiate
only one time.

Other alternative is to use namespaces but this is only for PHP 5.3 or you
can do what everyone else does from the begging of time use some prefix to
yours classes to avoid name conflicts.

Uacaman

2008/10/22 viraj <[email protected]>

> i'm trying to dynamically initialize bunch of classes from different
> paths but with the same class and file names.
>
> while ($register->request->m != 'exit') {
> $controlFile = './core/' . $register->request->m .
> '/controller.class.php';
>
> if (is_file($controlFile)) {
> require_once ($controlFile);
> $controller = new controller($register);
> }
> }
>
> the while loop should continue till one of those classes set 'exit' as
> the $register->request->m value, where it exists loop and reach the
> eos.
>
> but i get this fatal error.. Cannot redeclare class controller in
> /home/vir blah blah..
>
> yeah, i know it's because i try to declare the same class name several
> times.
>
> my question is.. what is the best way to implement this?
>
> many thanks
>
> ~viraj
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Sorry, only registered users may post in this forum.

Click here to login