Page 1 of 1
Dynamically Creating Classes
Posted: Wed Jun 08, 2005 3:38 am
by MathewByrne
Hi,
I'm creating a content management system in php and was wondering if it's possible to dynamically create a class depending on the name of a variable. Something like this:
Code: Select all
function createClass($class) {
$newClass = new $class();
}
Basically each section of the site has a class representing it. They all extend a 'Content' class. So when a user enters a page I want to create an instance of the class attached to the page they want to see. If the above is not possible then does anyone have any better solutions?
Posted: Wed Jun 08, 2005 3:59 am
by Syranide
my recommendation is to not use such functionality as it could be wuite harmful and generate lots of errors and problems if being able to abuse, my recommendation is a factory-class/method, described in the PHP5-migration, or simply, it is a class which is used for creating classes of a certain type as Content, it has a method which takes a "name"/"id", and it creates and returns the proper class for that "id", thus limiting the users interaction with the code.
(however, it is only available by using eval I think, but that has serious drawbacks -- for creating classes without a factory that is)
Posted: Wed Jun 08, 2005 4:24 am
by timvw
If i'm not mistaken
Code: Select all
$name = 'someclass';
$class = new $name;
Posted: Wed Jun 08, 2005 8:35 pm
by MathewByrne
Syranide wrote:my recommendation is to not use such functionality as it could be wuite harmful and generate lots of errors and problems if being able to abuse, my recommendation is a factory-class/method, described in the PHP5-migration, or simply, it is a class which is used for creating classes of a certain type as Content, it has a method which takes a "name"/"id", and it creates and returns the proper class for that "id", thus limiting the users interaction with the code.
(however, it is only available by using eval I think, but that has serious drawbacks -- for creating classes without a factory that is)
I think you're right. The server this will end up on only has PHP4 so I wont be able to use a factory. I think instead that I'll extend the base content maker class so provide spesific functionality for the classes I want to use.
Posted: Thu Jun 09, 2005 12:42 am
by Syranide
Uhm, actually you can do a factory in PHP4 too, it's just a concept of a class/method. (basically, what you did but instead of creating the class you have a switch-statement which checks the name and creates that class.
However, I hope that you are aware of nasty things that can happen with classes in PHP4 if you are not aware... any private variables within a class is not to be trusted, as if passed to a function, they are copied, and not referenced (by default)... meaning that you would now have two classes, one that has sent an email (e.g.), and when it returns again, you end up sending it again because no variable was modified.
Just watch up for it if you didn't know (or pass by reference and return by reference at all times). Or you can use a little hack I came up with that solves this problem, which makes the internal vars referenced instead (so only the reference is copied)