OldWolf Posted February 29, 2008 Share Posted February 29, 2008 The long and short of it is that I want to reclass a user object I've earlier created into an extended version of the previous class. The full explanation: I'm working on a user management system. It's basic purpose is to handle login, log out, registration, and protection of pages. One of the projects it will be apply to really needs more specific members, such as a variable storing their language, or a variable storing their template and it's path (templating will be handled with smarty... not really relevent, but I really like smarty, so I thought I'd toss that out there, lol). Is there a simple way to extend an existing object into a child class? The logics would be something like: user_class new object "sally" user_class do some stuff with sally (uh...) beefy_user_class, which extends user_class new object "beefy_sally" who has all of sally's class values I hope that's not horribly confusing... is there an easy way to do this... or does anyone have a better idea? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 29, 2008 Share Posted February 29, 2008 I don't believe there is a way to 'upgrade' an object that has already been constructed. But, you could do something like this in your beefy_user_class: <?php class beefy_user_class extends user_class { function __construct ( $user = false ) { if($user){ //Copy needed pieces of $user into $this }else{ //Not an upgrade } } //Other stuff } //Usage $user = new user_class(); //do some stuff $user = new beefy_user_class($user); //$user is now upgraded ?> Obviously the best solution would be to just use beefy_user_class from the beginning, but from your post, it sounds like that isn't possible. Quote Link to comment Share on other sites More sharing options...
aschk Posted February 29, 2008 Share Posted February 29, 2008 What is beefy? i.e. what does beefy give sally? Does it make sally really fat? e.g. her weight dbls? Sounds like the strategy or decorator pattern could help you structure this better but ideally I should know what "beefy" is, or does. Also, what is sally? Is it just the name of a user? In which case $name should be an attribute of the user class, NOT a separate class. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 29, 2008 Share Posted February 29, 2008 What is beefy? i.e. what does beefy give sally? Does it make sally really fat? e.g. her weight dbls? I met Sally, she is pretty overweight as it is. Can we make a slender_user_class? Quote Link to comment Share on other sites More sharing options...
OldWolf Posted February 29, 2008 Author Share Posted February 29, 2008 I don't believe there is a way to 'upgrade' an object that has already been constructed. But, you could do something like this in your beefy_user_class: <?php class beefy_user_class extends user_class { function __construct ( $user = false ) { if($user){ //Copy needed pieces of $user into $this }else{ //Not an upgrade } } //Other stuff } //Usage $user = new user_class(); //do some stuff $user = new beefy_user_class($user); //$user is now upgraded ?> Obviously the best solution would be to just use beefy_user_class from the beginning, but from your post, it sounds like that isn't possible. You're right, not the abosolute best way to go about it, but I think this is probably how I'll have to do it. Thanks by the way, I wouldn't have thought of using the constructor to transfer everything over. I do wish there was a better way, but I think this may be the only way of going about it. :/ What is beefy? i.e. what does beefy give sally? Does it make sally really fat? e.g. her weight dbls? Sounds like the strategy or decorator pattern could help you structure this better but ideally I should know what "beefy" is, or does. Also, what is sally? Is it just the name of a user? In which case $name should be an attribute of the user class, NOT a separate class. As I mentioned above, "beefy" is just a ridiculous example name I gave for my extended user class. The original user class, created by my user management system, will not contain the necessary methods and vars that I want the user class to have on that site. So what I'm trying to do is upgrade an existing object (sally is just an example of the object, in reality it would be $user) to the more advanced extended version of the user class unique to that particular website. I really don't want to have two separate user objects, nor do I want to modify the user class in my user management system since it will destroy the chance of independently upgrading that system as I make changes to it. :/ So essentially, beefy just adds methods and vars to the object sally, or if you prefer, $user. In a way, I suppose she does get fatter. Quote Link to comment Share on other sites More sharing options...
aschk Posted March 3, 2008 Share Posted March 3, 2008 What I was trying to determine was whether you were adding functionality, or adding a method that other classes (other than users could use) or altering methods that already exist. To be honest I don't think you should be extending the user class. Your user class will have variables inside it. These variables are the store for things like the name, age, weight, login privileges. e.g. <?php class user { private $name; private $age; public function __construct(){ } public function setName($name){ $this->name = $name; } public function setAge($age){ $this->age = $age; } public function getName(){ return $this->name; } public function getAge(){ return $this->age; } } ?> then you can do <?php $sally = new user(); $sally->setName("sally"); $sally->setAge(24); echo $sally->getName(); echo $sally->getAge(); ?> The above code will work out of box (i.e. right now in your PHP), so you can see how it works. Quote Link to comment Share on other sites More sharing options...
keeB Posted March 4, 2008 Share Posted March 4, 2008 I'm going to quote Allen Holub twice in the same week. My goodness! http://www.javaworld.com/javaworld/jw-08-2003/jw-0801-toolbox.html Read this, love it, learn it. I would do what you want to do in the following fashion: <?php interface User { //common functions } class BasicUser implements User { // functions which need to be implemented } class BeefyUser implements User { //what makes the beefy user different } ?> Quote Link to comment Share on other sites More sharing options...
Naez Posted March 4, 2008 Share Posted March 4, 2008 ^ KeeB that article certainly looks interesting, I just wish the examples were in PHP Quote Link to comment Share on other sites More sharing options...
aschk Posted March 4, 2008 Share Posted March 4, 2008 There are other ways. If "beefy" and "basic" are behaviours, then you can implement them using the strategy pattern. e.g. <?php abstract class UserBehaviour { abstract public function act(){} } class Beefy extends UserBehaviour { public function act(){ echo "i'm all beefy"; } } class Basic extends UserBehaviour { public function act(){ echo "i'm just basic"; } } class User { private $behaviour; public function setBehaviour(UserBehaviour $behaviour){ $this->behaviour = $behaviour; } public function act(){ $this->behaviour->act(); } } ?> <?php // Example $user = new User(); $user->setBehaviour(new Beefy()); $user->act(); // Change behaviour $user->setBehaviour(new Basic()); $user->act(); ?> Really it depends on the implementations that you're changing as to how you design your classes. Quote Link to comment Share on other sites More sharing options...
keeB Posted March 5, 2008 Share Posted March 5, 2008 ^ KeeB that article certainly looks interesting, I just wish the examples were in PHP What part is confusing and cannot be ported to PHP? I'll answer any questions you may have Quote Link to comment Share on other sites More sharing options...
Naez Posted March 5, 2008 Share Posted March 5, 2008 ^ KeeB that article certainly looks interesting, I just wish the examples were in PHP What part is confusing and cannot be ported to PHP? I'll answer any questions you may have Oh I was just saying how the example code he has is in Java. Once he started doing that it was hard for me to follow (having never touched Java). Quote Link to comment Share on other sites More sharing options...
keeB Posted March 5, 2008 Share Posted March 5, 2008 OOP is language independent.. it's just a completely different way of thinking. The (benefit/downfall -- you pick) of PHP is it's loosely typed. Meaning, return values aren't static, but can be an int, string, array.. which can (potentially) be cause for confusion for 'beginner' programmers when they want to do things 'correctly.' While the code from that article can't be directly ported, things like Syntax and explanations can be understood. If you're new to this I don't expect you to understand all of it right away -- I have been at this for a while and I still don't understand everything he says Programming (especially in OOP) is practice. You don't get anywhere if you are set on doing it right because you learn at such a quick pace. Your style and preferences will evolve over time. Happy coding! Quote Link to comment Share on other sites More sharing options...
OldWolf Posted March 6, 2008 Author Share Posted March 6, 2008 What I was trying to determine was whether you were adding functionality, or adding a method that other classes (other than users could use) or altering methods that already exist. To be honest I don't think you should be extending the user class. Your user class will have variables inside it. These variables are the store for things like the name, age, weight, login privileges. As I explained before, the basic user class does not contain all the methods and variables that I ultimately need for my user object. But, my user object will have already been created by the basic class. I need the extended version of the user class placed onto the object that already exists so that I have the additional methods and variables that extended class has. And I can't extend it before the object is created since the creation (among other things) is handled by my user management system. It's goofy, but other than on this project, it will work like a charm. :/ Quote Link to comment Share on other sites More sharing options...
aschk Posted March 6, 2008 Share Posted March 6, 2008 I think it would help to see some of the specifics of the implementation you're putting together. This should clarify what you're doing, and how to improve it. If you want to post your code (or some thereof) i'm sure others here will chip in with advice on the structure. Quote Link to comment Share on other sites More sharing options...
OldWolf Posted March 11, 2008 Author Share Posted March 11, 2008 I think it would help to see some of the specifics of the implementation you're putting together. This should clarify what you're doing, and how to improve it. If you want to post your code (or some thereof) i'm sure others here will chip in with advice on the structure. That's a little hard as I haven't written it yet. I'm trying to plan the structure before I start so I don't have to recode 4 times (which has happened before!). Quote Link to comment Share on other sites More sharing options...
aschk Posted March 14, 2008 Share Posted March 14, 2008 Everyone has to rewrite there stuff eventually when new requirements come along. The problem is we don't even know your full requirements yet so we can only guess at the functionality. Quote Link to comment Share on other sites More sharing options...
keeB Posted March 14, 2008 Share Posted March 14, 2008 As long as you program to an interface, with the idea that you can and will rip out entire implementations of an object (and not affect client code) -- You're on the right path. Good luck! Quote Link to comment Share on other sites More sharing options...
OldWolf Posted April 3, 2008 Author Share Posted April 3, 2008 I thought I would go back and say thank you, I think this is solved. Quote Link to comment 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.