Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Casting objects in PHP

Options
  • 30-05-2001 6:47pm
    #1
    Registered Users Posts: 4,676 ✭✭✭


    Howdy,

    Say i have 2 classes. A and B. B inherits from A.
    I create 2 objects, of type a and type b.

    $type_a = new a ;
    $type_b = new b ;

    Is there any way to cast type_a to type_b, retaining the values stored in type_a's variables ? I've seen a few people asking the question and cant find anything in the php manual that might do it.

    Anyone got any ideas ?
    Gav



Comments

  • Moderators, Social & Fun Moderators Posts: 28,633 Mod ✭✭✭✭Shiminay


    Maybe a swap class - set type_c as a's values, cast a and then pop the values back in?



    All the best!
    Dav
    @B^)
    We were all set for a game of Ice Hockey when Frank Williams says "Sorry lads, I've forgotten my skates!"
    [honey i] violated [the kids]
    Tribes 2 Goodness
    The Dawn of the Beefy King approaches...


  • Closed Accounts Posts: 218 ✭✭Void


    Warning: Unhelpful post:

    This sounds like the stuff that you can do in C++ but aren't really supposed to do because it's really bad. If you actually find yourself doing this in code then your object model is flawed.

    [This message has been edited by Void (edited 02-06-2001).]


  • Registered Users Posts: 568 ✭✭✭phil


    <font face="Verdana, Arial" size="2">Originally posted by Verb:
    Howdy,

    Say i have 2 classes. A and B. B inherits from A.
    I create 2 objects, of type a and type b.

    $type_a = new a ;
    $type_b = new b ;

    Is there any way to cast type_a to type_b, retaining the values stored in type_a's variables ? I've seen a few people asking the question and cant find anything in the php manual that might do it.

    Anyone got any ideas ?
    Gav

    </font>

    This doesn't work. PHP variables have basic types, i.e. String, int, Object.
    You can typecast from an int to an Object.

    If you want to copy over the values, create a function within a, which uses references to copy over the values into an object b.

    If you're still having trouble with the concept, I'll throw you up an example.
    But simply,
    a.copyValue(b);
    Is what I'm talking about.
    Phil.


Advertisement