How to test whether an instance is dynamic or static?

Asked

Viewed 279 times

5

Say you access an object you don’t know how it was created and you want to know if it was instantiated as new \Acme\Foo() or if it was just returned \Acme\Foo;

What is the most practical test ? (removed the requirement "without using Reflection")

Example:

<?php

$foo = App::getService('foo');

App::getService() returned an object that is stored in $foo;

$foo is a dynamic object?

  • @gmsantos see if the example helped ?

  • Why not use Reflection?

  • removed the requirement "without using Reflection"

3 answers

4

As far as I know, it is not possible to return reference to a PHP class without using Reflection. So, the scenario in which something would return \Acme\Foo, and not an instance, would depend on Reflection. Anyway, you can check if a variable contains an instance of a certain class with the operator instanceof:

class Foo {

}
$foo = new Foo();
$fooEhInstancia = $foo instanceof Foo; // true

A live example (repair the notice in stderr)

  • I don’t think this answers the question. It refers to the case of dynamic or static instances and not simply knowing the origin of an object.

  • True. Think you don’t know who you are Foobut you need to know if it’s dynamic or just static

  • 1

    What you mean by dynamic instance and static instance, @gpupo?

  • @Dynamic bfavaretto is an instantiated object with new and static the object in the global scope where we can access static properties and methods without polyformism

  • 1

    @gpupo There are no references to e classes, PHP. As I have already commented in the accepted answer, the reflection methods always return a String with the class name. However, what you can do is to know if the String represents a valid class with the method class_exists.

  • 1

    @utluiz for the unit test that I have in mind, just test if it is an unstable object is fine. But the explanation is very good

Show 1 more comment

3


It’s very simple! You can use is_object():

<?php
class Foo {

    public function hello()
    {
        return 'world';
    }

    public static function hi()
    {
        return 'Joe';
    }
}

$foo = new Foo();
$bar = Foo;

var_dump($foo instanceof Foo);//true;
var_dump($bar instanceof Foo);//false
var_dump(is_object($foo)); //true
var_dump(is_object($bar)); //false

echo $foo->hello(); //world
echo $bar::hi();//Joe
  • Great answer and simple. Gee, I didn’t remember this method!!!

  • In that example of yours, $bar contains a string, so gives false.

  • No @bfavaretto. $bar is Foo in its static form. See the improvement of my example?

  • $bar = Foo generates a notice, and the value is interpreted as string (http://ideone.com/F4jN2O). When you use the scope resolution operator ::, the class name is solved dynamically at runtime.

  • But this in a context of namespaces has different behavior.

  • @gpupo Resolution is always dynamic, as I know PHP does not have what you called "static instances" (I prefer to call "class references", and leave the term "instance" for objects created with new). Resolution depends on context. For example, $bar = \Acme\Foo is a syntax error, but $bar = new \Acme\Foo() is valid if the namespace and class exist - in which case the full name of the class is solved at runtime.

  • I just confirmed the @bfavaretto statement. PHP has no references to classes. All reflection API routines that return "classes" actually return Strings with their names. Examples: get_called_class, get_parent_class and get_declared_classes.

  • Interesting. But I wonder if my answer was invalidated. I still think is_object solves the case of @gpupo. What do you think?

  • @neoprofit I think it solves as much as my suggestion of instanceof (although it is not necessary to use the class name in your case). My comments were in order to clarify that there are no references to classes, as the utluiz said. If you use Reflection, you can even catch something similar, but that would be an instance of ReflectionClass (and therefore returns true with is_object).

  • I learned a lot today :D

Show 5 more comments

2

See in the example below:

<?php

class MyClass
{
  public function func1(){}
  public static function func2(){}
}

$reflection = new ReflectionClass('MyClass');

$func1 = $reflection->getMethod('func1');
$func2 = $reflection->getMethod('func2');

var_dump($func1->isStatic());
var_dump($func2->isStatic());

In it the class methods are tested MyClass. There are, two, one static (func2()) and other dynamic (func1()). That code has been obtained from link stackoverflow.

  • 2

    I don’t know why, but the question "without using Reflection"...

  • removed the requirement "without using Reflection"

Browser other questions tagged

You are not signed in. Login or sign up in order to post.