Universally define __call( ) ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MrKrinkle

    Universally define __call( ) ?


    I want to universally define the __call() method, so that every class
    in my application does the same thing if a non-existant function is
    called.

    In actionscript I could do this by adding the definition to the
    prototype for Object.

    Is there any way to do that in php 5? I could make every one of my
    classes inherit from like an Object class that I define, but that kind
    of sucks.

  • Oli Filth

    #2
    Re: Universally define __call( ) ?

    MrKrinkle said the following on 09/06/2005 20:38:[color=blue]
    > I want to universally define the __call() method, so that every class
    > in my application does the same thing if a non-existant function is
    > called.
    >
    > In actionscript I could do this by adding the definition to the
    > prototype for Object.
    >
    > Is there any way to do that in php 5? I could make every one of my
    > classes inherit from like an Object class that I define, but that kind
    > of sucks.
    >[/color]

    Hang on, you want to have a load of classes inherit a particular
    behaviour, but you don't want to specify that they extend a base class
    that defines this behaviour?? Why?

    --
    Oli

    Comment

    • MrKrinkle

      #3
      Re: Universally define __call( ) ?



      Oli Filth wrote:[color=blue]
      > MrKrinkle said the following on 09/06/2005 20:38:[color=green]
      > > I want to universally define the __call() method, so that every class
      > > in my application does the same thing if a non-existant function is
      > > called.
      > >
      > > In actionscript I could do this by adding the definition to the
      > > prototype for Object.
      > >
      > > Is there any way to do that in php 5? I could make every one of my
      > > classes inherit from like an Object class that I define, but that kind
      > > of sucks.
      > >[/color]
      >
      > Hang on, you want to have a load of classes inherit a particular
      > behaviour, but you don't want to specify that they extend a base class
      > that defines this behaviour?? Why?
      >
      > --
      > Oli[/color]

      Well, a couple reasons:

      A. I want EVERY object in the application to have this behavior.

      B. If I've already defined classes (and I have) then I would have to go
      back and add "extends MyBaseObject" to all of the root classes. Some
      of the classes come from other people, so each time they send me an
      update of their source, I'd have to modify it, or get them to do this
      for me anyway.

      C. Since I'm redefining/overriding the __call function, which is
      already on whatever "base" class php has for objects (what would be
      "Object" in java), then it makes sense to modify the prototype rather
      than invent my own Object class.

      In AS 2 this would be easy.

      In my experience, asking someone "but why would you want to do that" is
      never fruitful, it's usually a way to avoid trying to answer the
      question.

      Comment

      • Oli Filth

        #4
        Re: Universally define __call( ) ?

        MrKrinkle said the following on 11/06/2005 20:43:[color=blue]
        >
        > Oli Filth wrote:
        >[color=green]
        >>MrKrinkle said the following on 09/06/2005 20:38:
        >>[color=darkred]
        >>>I want to universally define the __call() method, so that every class
        >>>in my application does the same thing if a non-existant function is
        >>>called.
        >>>
        >>>In actionscript I could do this by adding the definition to the
        >>>prototype for Object.
        >>>
        >>>Is there any way to do that in php 5? I could make every one of my
        >>>classes inherit from like an Object class that I define, but that kind
        >>>of sucks.
        >>>[/color]
        >>
        >>Hang on, you want to have a load of classes inherit a particular
        >>behaviour, but you don't want to specify that they extend a base class
        >>that defines this behaviour?? Why?
        >>[/color]
        >
        > Well, a couple reasons:
        >
        > A. I want EVERY object in the application to have this behavior.
        >
        > B. If I've already defined classes (and I have) then I would have to go
        > back and add "extends MyBaseObject" to all of the root classes. Some
        > of the classes come from other people, so each time they send me an
        > update of their source, I'd have to modify it, or get them to do this
        > for me anyway.
        >
        > C. Since I'm redefining/overriding the __call function, which is
        > already on whatever "base" class php has for objects (what would be
        > "Object" in java), then it makes sense to modify the prototype rather
        > than invent my own Object class.[/color]

        AFAIK, PHP classes are standalone and are not implicitly derived from a
        built-in base class (i.e. not like in Java, for instance). There is no
        predefined behaviour for __call().
        [color=blue]
        > In AS 2 this would be easy.
        >
        > In my experience, asking someone "but why would you want to do that" is
        > never fruitful, it's usually a way to avoid trying to answer the
        > question.
        >[/color]

        The answer was implicit in my question: you can't!

        PHP (like most OOP languages) is class-based, rather than prototype-based.

        If you want a set of classes to share common (customised) functionality,
        then you must derive them from a common base class.



        --
        Oli

        Comment

        • MrKrinkle

          #5
          Re: Universally define __call( ) ?


          Well I don't see how your response implied that answer but thank you
          just the same.

          Comment

          Working...