Jump to content

[SOLVED] Reclassing an object? :/


OldWolf

Recommended Posts

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

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.  :P

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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
}

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

^ 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).

Link to comment
Share on other sites

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!

 

 

Link to comment
Share on other sites

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.  :/

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!).  ;)

Link to comment
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.