Grayda Posted July 14, 2008 Share Posted July 14, 2008 In my latest PHP project, I have a few classes that perform various things, for example, authenticating users, pulling information from databases and so forth. One of the big points of my project is allowing trusted users to eval() code, so they can integrate existing PHP code into their website. What I would like, is for trusted users to be able to "overwrite" parts of the code at run-time. For example, if this is my security class: <?php class Security { ... function authenticateUser($user, $password) { // Code to authenticate user goes here } ... } ?> I would like users to be able to do write something like this: <?php class Security { ... function authenticateUsers($user, $password) { // A different authentication scheme in here } ... } ?> Much like "class NewSecurity extends Security", but instead of creating an extended class, merely re-defining / re-using existing methods in an existing class I suppose this is more of a personal curiosity thing rather than a serious idea for my application (although, releasing patches without editing real files on the server, has it's advantages ), but the question is, can it be done? Quote Link to comment https://forums.phpfreaks.com/topic/114617-modifying-existing-class/ Share on other sites More sharing options...
ignace Posted July 23, 2008 Share Posted July 23, 2008 your problem can be solved using the abstract factory pattern abstract class Security { abstract public function authenticateUser($username, $password); public static function factory($username, $password, $auth) { if ($auth == "http") { $class = new HttpAuth(); /** * this is why you need it to be an abstract factory, so you can be sure that the class will have a authenticateUser() method * in addition you can do, after initiation: if (is_a($class, 'Security')) to be sure the class will have a authenticateUser() method */ $class->authenticateUser($username, $password); } ... } } class HttpAuth extends Security { public function authenticateUser($username, $password) {/* authentication method 1 */} } class FormAuth extends Security { public function authenticateUser($username, $password) {/* authentication method 2 */} } in your script then: $authentication_method = ... Security::factory($username, $password, $authentication_method); // depending on $authentication_method HttpAuth or FormAuth will be initiated Quote Link to comment https://forums.phpfreaks.com/topic/114617-modifying-existing-class/#findComment-597371 Share on other sites More sharing options...
Grayda Posted August 12, 2008 Author Share Posted August 12, 2008 Thanks for the help ignace, but I'm still a little confused. Can I have: <?php // This is in base.php which is a file on my web-server abstract class Person { abstract public function getRealName($username); } ?> and <?php // This is code that a user copies and pastes into the online editor. This page is stored within a DATABASE so it needs to be eval()'d when it's grabbed from the database and the user is authenticated class Person { function getRealName($username) { echo "Hi, your real name is: " . $someclass->somefunction; } } ?> or do I still need to extend classes? Also, what about a default implementation? Could I have something like (and remember that this is pseudo-code and I don't know if this can or cannot be done): <?php // This goes into base.php which is located in a directory on the webserver abstract class Person { abstract public function getRealName($username) { echo "Hi, your real name is: " . $someOtherClass->someOtherFunction($username); } } ?> and: <?php // This is what gets copied and pasted into the database, so it must be eval()'d class Person { function getRealName($username) { return $someOtherClass->someOtherFunction($username); } } ?> So that way if no overwriting is done, it will use the default method, but if one is specified, it'll use that? I've seen Runkit for PHP (http://au2.php.net/manual/en/intro.runkit.php), which lets you do this, but it also provides extra stuff which I might not need, which is why I wondered if there was a native way to do this in PHP5 I hope this makes sense, and thanks for taking the time to help out Quote Link to comment https://forums.phpfreaks.com/topic/114617-modifying-existing-class/#findComment-614182 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.