0

I am wanting to be able to do something to the order of this

$name = "Hello".time().mt_rand();
class $name {
    function hellolol() {
        echo "LOL";
    }
}
$name::hellolol();

So that I can build a reload-able module system in PHP. Is this even possible, and if so, how would I go about doing something like this?

EDIT: I've since turned this into a project that essentially does what the accepted answer suggested. Here's a link: https://github.com/Modfwango/Modfwango

15
  • 1
    Why is that necessary to build a "reload-able module system"? What is a "reload-able module system"? Commented Nov 22, 2012 at 2:06
  • Are you sure your method name is fixed ;-) Commented Nov 22, 2012 at 2:07
  • Since you cannot redefine classes, I need to be able to have a dynamic name for the class. This will allow me to include a file, initialize the function, and register the classname in a module management class. Commented Nov 22, 2012 at 2:07
  • 4
    @ClayFreeman: Why do you need a dynamic name, though? Why do you even need separate classes? Commented Nov 22, 2012 at 2:11
  • 1
    @ClayFreeman: Sorry, not specific enough :) I really don't see how that changes anything. Do you mean you want to update a file and include it again? (How are you running PHP in such a way that that's even possible?) Commented Nov 22, 2012 at 2:18

2 Answers 2

4
$name = "Hello".time().mt_rand();
eval(sprintf('
class %s {
    static function hellolol() {
        echo "LOL";
    }
}', $name));
$name::hellolol();

Pretty nasty but good for mocking, etc.

Sign up to request clarification or add additional context in comments.

5 Comments

This should work, and I appreciate the answer, but I am weary that this will show up horribly in my text editor.
It probably will. Trick it by using a template, place the content in a .php file with the definition class @@CLASSNAME@@ and use eval(str_replace('@@CLASSNAME@@', $class_name, file_get_contents('your_template.php')));
I am probably going to end up doing that.
Looks familiar! Keep in mind that eval parses the given string as though it's already prefixed by <?php, so you'll need to use eval('?>'.[...] if the template file starts with <?php
@Adam: did you invent sed as well? ^^ I just saw this eval-method in the wild, check out Kohana2's autoloader. I'm gonna be sick..
3

This technically is possible, just in a very, very bad way.

First, save this file:

<?php
// GeneratedClass.php
class CLASSNAME {
    function hellolol(){
        echo "LOL";
    }
}

Then, use the following to create classes with custom names:

<?php
function generate_class($name){
    eval('?>'.str_replace('CLASSNAME', $name, file_get_contents('GeneratedClass.php')));
}

generate_class("Hello".time().mt_rand());

Again, this is not a good idea! Aside from the fact that anything to do with eval is probably a bad idea, by parsing these classes manually, you'd lose any advantages an IDE would give you, the files wouldn't be cacheable by something like memcached, and it's just altogether a nightmare to use. But it's possible.

5 Comments

I like this answer. This may be how I set this up. I am going to use something to this nature to make a reload-able module system for an IRC bot. This will allow me to instantiate new classes for commands when they are reloaded so that I won't have to restart the whole process for minor changes to things.
Hopefully it works for you! A long time ago I had a similar setup to this going to let me override classes throughout an application, or generate a class if the override didn't exist...I've since realised that wasn't a good idea, and come up with better ways to go about it, so make sure it's the best option for what you're trying to achieve!
I hope this works. I think that it will work for me. I will throw away the old classes when they are old, etc.
If the classes you're generating are going to be used more than once, consider saving them to a file instead of evaling, so then you can include them like normal, and save having to reload the original generation file each time.
Generation will be part of the class for managing the modules that I load.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.