[英]check if item can be converted to string?
I am writing a debug method.我正在编写调试方法。
What I have is我拥有的是
if(is_xxx($item)){
//echo output info for type
}
what I want to do at the end is最后我想做的是
if(can_be_string($item))
echo $item;
Is there a can_be_string
type function?是否有
can_be_string
类型的函数?
Ok, edited, with incorporating Michiel Pater 's suggestion (who's answer is gone now) ans @eisberg's suggestions.好的,已编辑,并结合了Michiel Pater的建议(现在谁的答案消失了)和 @eisberg 的建议。
settype
will return true
with objects no matter what, as it seems. settype
将返回true
对象,看起来。
if(
( !is_array( $item ) ) &&
( ( !is_object( $item ) && settype( $item, 'string' ) !== false ) ||
( is_object( $item ) && method_exists( $item, '__toString' ) ) )
)
{
echo $item;
}
For the sake of completion...为了完成...
http://www.php.net/is_scalar , available since PHP 4; http://www.php.net/is_scalar ,自 PHP 4 起可用; not a single word about it.. :)
没有一个字关于它.. :)
How about怎么样
function can_be_string($var) {
return $var === null || is_scalar($var) || (is_object($var) && method_exists($var, '__toString'));
}
And since PHP 8 you can replace the third condition with $var instanceof Stringable
从PHP 8 开始,你可以用
$var instanceof Stringable
替换第三个条件
Quick and dirty implementation with test:使用测试快速而肮脏的实现:
function canBeString($value)
{
if (is_object($value) and method_exists($value, '__toString')) return true;
if (is_null($value)) return true;
return is_scalar($value);
}
class MyClass
{
}
$object = new MyClass();
var_dump(canBeString($object)); // bool(false)
class MyClassWithToString
{
public function __toString()
{
return 'foo';
}
}
$objectWithToString = new MyClassWithToString();
var_dump(canBeString($objectWithToString)); // bool(true)
var_dump(canBeString(1)); // bool(true)
echo (string)1 . "\n";
var_dump(canBeString(false)); // bool(true)
echo (string)true . "\n";
var_dump(canBeString(1.0)); // bool(true)
echo (string)1.0 . "\n";
var_dump(canBeString(null)); // bool(false)
var_dump((string)null); // string(0) ""
Here is the shortest way I can find:这是我能找到的最短方法:
/**
* Determine if we can cast a value to a string.
*
* @param mixed $v
*
* @return bool
*/
function can_be_string($v): bool
{
return method_exists($v, '__toString') || $v === null || is_scalar($v);
}
how about this decision?这个决定怎么样?
function canBeString ($value) {
try {
$value = (string) $value;
} catch (\ErrorException $exception) {
return false;
}
return true;
}
If is_string() doesn't help you, then I can only suggest some ideas:如果 is_string() 对您没有帮助,那么我只能提出一些建议:
It's very simple:这很简单:
function can_be_string($var)
{
return method_exists($var, '__toString') || (is_scalar($var) && !is_null($var));
}
The code has already been tested.代码已经测试过了。 The
method_exists()
function returns false
if $var
is not an object, so this verification is not necessary.如果
$var
不是对象, method_exists()
函数将返回false
,因此无需进行此验证。
I didn't think any of the answers were satisfactory.我不认为任何答案是令人满意的。
Simplest implementation:最简单的实现:
function can_be_string($value) {
return (!is_object($value) && !is_array($value))
|| method_exists($value, '__toString');
}
class IsString {
public function __toString() {
return 'hello';
}
}
var_dump(
can_be_string(null), // true
can_be_string(123), // true
can_be_string(12.3), // true
can_be_string('hello'), // true
can_be_string(new IsString), // true
can_be_string(new stdClass), // false
can_be_string([1, 2, 3]) // false
);
With print_r()
and var_dump()
functions you can print-out contents of any variable.使用
print_r()
和var_dump()
函数,您可以打印出任何变量的内容。
In case you don't like the output of mentioned functions, there is a plenty of type checking functions here http://www.php.net/manual/en/ref.var.php如果您不喜欢上述函数的输出,这里有很多类型检查函数http://www.php.net/manual/en/ref.var.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.