3

Is there any official/recommended way to declare/define/register a new class at runtime in PHP?

What I basically want to do is generating Classes based on the definition stored in a database, both properties and methods.

I have found some article/threads, but all of them are actually old, so I was wondering if it exists some elegant/efficient way to do it. My surprise is that there is no so much information about this as I thought.

Is it so bad? As far as I know, PHP always saves the class definition in memory so I don't see any problem in declaring the class at runtime as it's basically the same result, but maybe I'm wrong.

I think is not relevant right now, but I'm working with Laravel in this project.

Thanks!

1 Answer 1

3

Yes.

<?php

$foobar = "foobar";

eval("class $foobar {" .
        "function __construct(\$i) {" .
                "echo \"Dark magic ritual complete. \$i virgins sacrificed to satan.\"; " .
        "}" . 
" }");

$evil = new $foobar(5);


echo "<br /><br />";

var_dump($evil);

Produces:

Dark magic ritual complete. 5 virgins sacrificed to satan.

object(foobar)#1 (0) { }

I'm not going to lecture you on semantics, but this is a terribly bad idea and you should reconsider using classes to facilitate what the database records need. A class by itself is not special, but using a class to handle a database entry is perfectly acceptable.

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

13 Comments

I think he is creating classes based on values stored in the database, not something equivalent as ORM.
Either I need a lot more caffeine or "declare/define/register" has a different meaning to me. It's trivial to instantiate a new, which would indicate he wants something more complicated.
Thanks for your answer. What I actually need is to work with dynamic entities, as the different users should be able to create their own objects with its attributes. I have decided not to use Entity-Attribute-Value "anti"-pattern because of the complexity and all the cons it has, so I thought this could be a better solution. I tried to ask which was the best approach/pattern for this in another question but It didn't work as it was not a Php specifc question. So, which would be the recommendation for this?
As I've shown, you can use new $foobar where $foobar is a class name to instantiate an existing class. In an anecdotal experience, I've had a User Password table where a user's password could have many different kinds of encryption algorithms. To solve password checking, I wrote a Password interface and then PasswordMd5 PasswordSha256 classes that implement the Interface and instantiate based on a complementary encryption column. $goodPassword = new $encryption()->is($inputPassword).
That question, by the way, is the one you should have been asking. :P
|

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.