简体   繁体   English

以字符串形式访问对象两次

[英]Accessing an object twice in string

I've noticed this in PHP and MySQL when I've been trying to put some object values in my query. 当我一直试图在查询中添加一些对象值时,我在PHP和MySQL中注意到了这一点。 I did some google searching for "Accessing object property twice" and "-> PHP twice", but couldn't find much results. 我做了一些谷歌搜索“两次访问对象属性”和“ - > PHP两次”,但找不到太多结果。 I think it's the way PHP processes strings and wanted a bit more information. 我认为这是PHP处理字符串并希望获得更多信息的方式。 This is my question: 这是我的问题:

<?php
class Object {
    public function __construct() {
        $this->a = new A();
        echo "This x is '$this->a->x'";
    }
}

class A {
    public function __construct() {
        $this->x = 1;
    }
}

$object = new Object();
?>

The above code will raise an error: E_RECOVERABLE_ERROR : type 4096 -- Object of class A could not be converted to string -- at line 5 上面的代码将引发错误: E_RECOVERABLE_ERROR : type 4096 -- Object of class A could not be converted to string -- at line 5

However, if we give it a temporary variable like this: 但是,如果我们给它一个像这样的临时变量:

<?php
class Object {
    public function __construct() {
        $this->a = new A();
        $x = $this->a->x;
        echo "This x is '$x'";
    }
}

class A {
    public function __construct() {
        $this->x = 1;
    }
}

$object = new Object();
?>

Then it works completely fine, or if we concatenate it, it also works fine 然后它完全正常,或者如果我们连接它,它也可以正常工作

<?php
class Object {
    public function __construct() {
        $this->a = new A();
        echo "This x is '" . $this->a->x . "'";
    }
}
?>

Double quoted PHP strings are supposed to be able to recognize variables prefixed with $ and automtically substitute them. 双引号PHP字符串应该能够识别前缀为$变量并自动替换它们。 This also works fine if we're only accessing the object property once ( -> single). 如果我们只访问一次object属性( -> single),这也可以正常工作。 I think the PHP double-quoted string stop processing after a single -> thus the reason for th error: 我认为PHP双引号字符串停止处理后单个->因此出错的原因:

E_RECOVERABLE_ERROR : type 4096 -- Object of class A could not be converted to string -- at line 5

Which makes sense if it is only being accessed once (since $this->a is still an object). 如果它只被访问一次是有意义的(因为$this->a仍然是一个对象)。 I'm not entirely convinced and could not find much information on this in PHP, so I was wondering if anyone could elaborate or detail me more on this and why it happens? 我并不完全相信,也无法在PHP中找到关于此的更多信息,所以我想知道是否有人可以详细说明或详细说明我以及为什么会发生这种情况?

You are correct. 你是对的。 PHP only takes $this->a as variable. PHP只需要$this->a作为变量。 You can see this also in the PHP manual : 你也可以在PHP手册中看到这个:

Similarly, an array index or an object property can be parsed. 类似地,可以解析数组索引或对象属性。 With array indices, the closing square bracket (]) marks the end of the index . 对于数组索引, 结束方括号(])标记索引的结尾 The same rules apply to object properties as to simple variables. 对于简单变量, 相同的规则适用于对象属性

This means in a double quoted string: 这意味着在双引号字符串中:

$arr[0][1] = "X"; 
echo "$arr[0][1]";  //Same as echo $arr[0] . "[1]";

$o = (object)["a" => (object)["b" => "X"]];
echo "$o->a->b";    //Same as echo $o->a "->b";

So to solve this you have to use complex curly syntax, eg 所以要解决这个问题,你必须使用复杂的卷曲语法,例如

echo "{$o->a->b}";
      ↑See here↑ 

With this you can explicit define what the variable is and what normal string is. 有了这个,您可以明确定义变量是什么以及普通字符串是什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM