Is there anyway to see if an object specifically implements ->__toString? This doesn't seem to work:
反正有没有看到一个对象是否专门实现 - > __ toString?这似乎不起作用:
method_exists($object, '__toString');
4 个解决方案
#1
10
There are two way to check it.
有两种方法可以检查它。
Lets assume you have classes:
让我们假设您有课程:
class Foo
{
public function __toString()
{
return 'foobar';
}
}
class Bar
{
}
Then you can do either:
然后你可以做到:
$rc = new ReflectionClass('Foo');
var_dump($rc->hasMethod('__toString'));
$rc = new ReflectionClass('Bar');
var_dump($rc->hasMethod('__toString'));
or use:
$fo = new Foo;
var_dump( method_exists($fo , '__toString'));
$ba = new Bar;
var_dump( method_exists($ba , '__toString'));
Difference is that in first case the class is not actually instantiated.
You can look at demo here : http://codepad.viper-7.com/B0EjOK
区别在于,在第一种情况下,该类实际上并未实例化。您可以在这里查看演示:http://codepad.viper-7.com/B0EjOK
#2
3
I must be doing something wrong somewhere else, because this works:
我必须在其他地方做错事,因为这有效:
class Test {
function __toString() {
return 'Test';
}
}
$test = new Test();
echo method_exists($test, '__toString');
#3
2
You should be able to use reflection: http://www.php.net/manual/en/reflectionclass.hasmethod.php
你应该能够使用反射:http://www.php.net/manual/en/reflectionclass.hasmethod.php
#4
2
Reflections is slow, and I think it's the worst solution to use them.
反思很慢,我认为这是使用它们的最糟糕的解决方案。
bool method_exists ( mixed $object , string $method_name )
object - An object instance or a class name (http://php.net/manual/en/function.method-exists.php)
object - 对象实例或类名(http://php.net/manual/en/function.method-exists.php)
There is no need to create an object to checking for existence of a method.
无需创建对象来检查方法是否存在。
method_exists('foo', '__toString')
or
interface StringInterface{
public function __toString() :string;
}
class Foo implement StringInterface {...}
->>(new MyClass) instanceof StringInterface
#1
10
There are two way to check it.
有两种方法可以检查它。
Lets assume you have classes:
让我们假设您有课程:
class Foo
{
public function __toString()
{
return 'foobar';
}
}
class Bar
{
}
Then you can do either:
然后你可以做到:
$rc = new ReflectionClass('Foo');
var_dump($rc->hasMethod('__toString'));
$rc = new ReflectionClass('Bar');
var_dump($rc->hasMethod('__toString'));
or use:
$fo = new Foo;
var_dump( method_exists($fo , '__toString'));
$ba = new Bar;
var_dump( method_exists($ba , '__toString'));
Difference is that in first case the class is not actually instantiated.
You can look at demo here : http://codepad.viper-7.com/B0EjOK
区别在于,在第一种情况下,该类实际上并未实例化。您可以在这里查看演示:http://codepad.viper-7.com/B0EjOK
#2
3
I must be doing something wrong somewhere else, because this works:
我必须在其他地方做错事,因为这有效:
class Test {
function __toString() {
return 'Test';
}
}
$test = new Test();
echo method_exists($test, '__toString');
#3
2
You should be able to use reflection: http://www.php.net/manual/en/reflectionclass.hasmethod.php
你应该能够使用反射:http://www.php.net/manual/en/reflectionclass.hasmethod.php
#4
2
Reflections is slow, and I think it's the worst solution to use them.
反思很慢,我认为这是使用它们的最糟糕的解决方案。
bool method_exists ( mixed $object , string $method_name )
object - An object instance or a class name (http://php.net/manual/en/function.method-exists.php)
object - 对象实例或类名(http://php.net/manual/en/function.method-exists.php)
There is no need to create an object to checking for existence of a method.
无需创建对象来检查方法是否存在。
method_exists('foo', '__toString')
or
interface StringInterface{
public function __toString() :string;
}
class Foo implement StringInterface {...}
->>(new MyClass) instanceof StringInterface