Dynamically Creating Classes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
MathewByrne
Forum Commoner
Posts: 38
Joined: Sat Mar 27, 2004 9:49 pm
Location: Australia

Dynamically Creating Classes

Post 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?
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post 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)
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

If i'm not mistaken

Code: Select all

$name = 'someclass';
$class = new $name;
User avatar
MathewByrne
Forum Commoner
Posts: 38
Joined: Sat Mar 27, 2004 9:49 pm
Location: Australia

Post 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.
Syranide
Forum Contributor
Posts: 281
Joined: Fri May 20, 2005 3:16 pm
Location: Sweden

Post 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)
Post Reply