SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    996
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)

    Hinting "object"?

    You can hint "array" or a specific class, is there any way to hint that *any* object can be used? Essentially I'm using reflection in order to work out what methods are available. Obviously reflection only works on objects so it would be nice to be able to type hint like this:

    PHP Code:
    function foo(object $bar) {

    And while this code parses even without "object" being defined (I guess it's defined internally by php). As soon as you attempt to pass an object to the function, it errors because it's not of type "object".

    Any ideas if this is possible? The only other alternative I can think of is:

    PHP Code:
    function foo($bar) {
    if (!
    is_object($bar)) throw new Exception('$bar must be of type Object');

    Thanks!

  2. #2
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    I believe your second approach would be the preferred approach. It is my understanding that type hinting in PHP is really a way to apply constraints to your functions, meaning the object passed to that function MUST match the hinted type.

    So in your first example, you would have to cast your regular object to (object), while passing it into your function.
    PHP Code:
    function foo(object $bar) { 
    }  
    $example = new Example();
    foo((object)$example); 
    Casting is considered an expensive process in most languages, so it is usually best to avoid it if possible.

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You would have to do something like your second piece of code.

    For your first way, these would work for example:

    PHP Code:
    function fooException $bar ) {}
    function 
    foostdClass $bar ) {}
    function 
    foomyDefinedClass $bar ) {} 
    In those examples $bar must be an instance of the type. So if the object you're passing in must be an instance (or decendant) of a particular class, you can use that. Otherwise, do an if statement inside as you posted.
    - Robert

  4. #4
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    996
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by cpradio View Post
    I believe your second approach would be the preferred approach. It is my understanding that type hinting in PHP is really a way to apply constraints to your functions, meaning the object passed to that function MUST match the hinted type.

    So in your first example, you would have to cast your regular object to (object), while passing it into your function.
    PHP Code:
    function foo(object $bar) { 
    }  
    $example = new Example();
    foo((object)$example); 
    Casting is considered an expensive process in most languages, so it is usually best to avoid it if possible.
    Interesting, but doesn't casting it to (object) remove its behaviour? I want to keep it as is so I can use reflection to see what methods are available on it. Even if it doesn't, casting is ugly, and forcing casting each time the function is called is messy! The is_object() method is tidier I think!



    Quote Originally Posted by QMonkey View Post
    You would have to do something like your second piece of code.

    For your first way, these would work for example:

    PHP Code:
    function fooException $bar ) {}
    function 
    foostdClass $bar ) {}
    function 
    foomyDefinedClass $bar ) {} 
    In those examples $bar must be an instance of the type. So if the object you're passing in must be an instance (or decendant) of a particular class, you can use that. Otherwise, do an if statement inside as you posted.
    My question was that I wanted to be able to pass any kind of object, but *any* object, just not an array or primitive.

  5. #5
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    PHP Code:
    class Bar {}

    function 
    foostdClass $bar ) {
    return 
    1;
    }

    $b = new Bar;

    echo 
    foo($b); 
    That only works if I use up the chance of extending, and would be terribly restrictive.

    PHP Code:
    Bar extends stdClass {} 
    ... that surprised me a bit, because I understood that in some languages all classes are derived from a basic class (Java?).

    I wonder if all PHP classes maybe implement a shared interface?

  6. #6
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    I wonder if all PHP classes maybe implement a shared interface?
    No. That's why you can't do what the OP is attempting. And the first reply is not right. You just can't type hint for an object unless you know the type of object it is. "object" or "stdClass" or anything is not a base which all PHP objects extend; there is no such base.
    - Robert

  7. #7
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,234
    Mentioned
    154 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by QMonkey View Post
    No. That's why you can't do what the OP is attempting. And the first reply is not right. You just can't type hint for an object unless you know the type of object it is. "object" or "stdClass" or anything is not a base which all PHP objects extend; there is no such base.
    The first reply was right at the beginning (emphasis added to show the initial response was spot on), the "first" approach may be flawed, but I never recommended that he perform the first approach.
    I believe your second approach would be the preferred approach. It is my understanding that type hinting in PHP is really a way to apply constraints to your functions, meaning the object passed to that function MUST match the hinted type.
    The second part of my response was simply from my .NET knowledge where such a technique could be utilized (didn't realize PHP didn't permit this capability).


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •