downcasting in php

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ryan  van Roode

    #1

    downcasting in php

    Hello,

    An API function gives me an object of class 'Blah'. I have subclassed
    'Blah' and added a few methods:

    class EnhancedBlah extends Blah {
    function enhancement1() {}
    function enhancement2() {}
    }

    Is there a way to 'downcast' the object to a subclass? Something like
    this:

    $blah = SuperApi::getBl ah();
    $enhancedBlah = (EnhancedBlah) $blah;

    Thanks for your help.

    ~RvR

  • Willem Bogaerts

    #2
    Re: downcasting in php

    An API function gives me an object of class 'Blah'. I have subclassed
    'Blah' and added a few methods:
    >
    class EnhancedBlah extends Blah {
    function enhancement1() {}
    function enhancement2() {}
    }
    >
    Is there a way to 'downcast' the object to a subclass? Something like
    this:
    >
    $blah = SuperApi::getBl ah();
    $enhancedBlah = (EnhancedBlah) $blah;
    No. That would break the substitution principle. It is the other way
    around. You can create an instance of the subclass and treat it like you
    would treat an instance of the superclass. Subclasses are more detailed
    than superclasses, so you would need extra info when downcasting. In
    fact, superclasses are usually generic terms, and therefore abstract
    (non-technical meaning here), even if they don't have abstract methods
    in the technical sense.

    An everyday example:
    The superclass "policeman" has subclasses "inspector" , "traffic
    policeman", etc. The common functionality gets in the superclass. "make
    arrest", for example. You can ask any member of a specific subclass to
    perform his duty as a member of the superclass. But not the other way
    around. In fact, there is no such thing as a "policeman" instance: every
    policeman is a specific member of a subclass of "policeman" .

    Best regards.
    --
    Willem Bogaerts

    Application smith
    Kratz B.V.

    Comment

    • Schraalhans Keukenmeester

      #3
      Re: downcasting in php

      At Thu, 24 May 2007 23:07:18 -0700, Ryan van Roode let his monkeys type:
      Hello,
      >
      An API function gives me an object of class 'Blah'. I have subclassed
      'Blah' and added a few methods:
      >
      class EnhancedBlah extends Blah {
      function enhancement1() {}
      function enhancement2() {}
      }
      >
      Is there a way to 'downcast' the object to a subclass? Something like
      this:
      >
      $blah = SuperApi::getBl ah();
      $enhancedBlah = (EnhancedBlah) $blah;
      >
      Thanks for your help.
      >
      ~RvR
      Nope. Simple reason: Every cat (ExtendedBlah) is a mammal (Blah), not
      every mammal (Blah) a cat (ExtendedBlah).
      So you can treat Enhancedblah as a Blah, but not the other way around.

      HTH
      Sh.

      Comment

      • gosha bine

        #4
        Re: downcasting in php

        On 25.05.2007 08:07 Ryan van Roode wrote:
        Hello,
        >
        An API function gives me an object of class 'Blah'. I have subclassed
        'Blah' and added a few methods:
        >
        class EnhancedBlah extends Blah {
        function enhancement1() {}
        function enhancement2() {}
        }
        >
        Is there a way to 'downcast' the object to a subclass? Something like
        this:
        >
        $blah = SuperApi::getBl ah();
        $enhancedBlah = (EnhancedBlah) $blah;
        >
        Thanks for your help.
        >
        ~RvR
        >
        Technically that's possible, see e.g. http://www.tagarga.com/blok/on/061130

        but as others already said, the very idea is wrong.


        --
        gosha bine

        extended php parser ~ http://code.google.com/p/pihipi
        blok ~ http://www.tagarga.com/blok

        Comment

        • Schraalhans Keukenmeester

          #5
          Re: downcasting in php

          At Fri, 25 May 2007 10:18:48 +0200, gosha bine let his monkeys type:
          On 25.05.2007 08:07 Ryan van Roode wrote:
          >Hello,
          >>
          >An API function gives me an object of class 'Blah'. I have subclassed
          >'Blah' and added a few methods:
          >>
          > class EnhancedBlah extends Blah {
          > function enhancement1() {}
          > function enhancement2() {}
          > }
          >>
          >Is there a way to 'downcast' the object to a subclass? Something like
          >this:
          >>
          > $blah = SuperApi::getBl ah();
          > $enhancedBlah = (EnhancedBlah) $blah;
          >>
          >Thanks for your help.
          >>
          >~RvR
          >>
          >
          Technically that's possible, see e.g. http://www.tagarga.com/blok/on/061130
          >
          but as others already said, the very idea is wrong.
          The effect may be comparable to a sort of cast, but it isn't. It's simply
          (ab)using the serialize function to copy just the vars/values and not the
          methods of another class.

          If OP has a need to do such a thing he probably should loook at his class
          design compared to what he was trying to achieve at a more abstract level.
          Good chance his original design is flawed.

          Sh.
          Sh.

          Comment

          • justin.demaris@gmail.com

            #6
            Re: downcasting in php

            The trick I usually use if I need to do something similar to that is
            to create a constructor for the subclass that takes the parent class
            as its only parameter. Then you just do

            $obj = new Blah();
            $obj = new EnhancedBlah($o bj);

            Same type of effect and you can still use the $obj anywhere you have
            before because its a subclass now.

            On May 25, 2:07 am, Ryan van Roode <rvanro...@gmai l.comwrote:
            Hello,
            >
            An API function gives me an object of class 'Blah'. I have subclassed
            'Blah' and added a few methods:
            >
            class EnhancedBlah extends Blah {
            function enhancement1() {}
            function enhancement2() {}
            }
            >
            Is there a way to 'downcast' the object to a subclass? Something like
            this:
            >
            $blah = SuperApi::getBl ah();
            $enhancedBlah = (EnhancedBlah) $blah;
            >
            Thanks for your help.
            >
            ~RvR

            Comment

            Working...