Questions about the PHP constructor

Source: Internet
Author: User
class Test{    public $client;        public function __construct($obj)    {        $this->client = new Obj();         //    ......        //  一系列操作    }}// 代码1$client = (new Test())->client;$client->method(); //报错//代码2$test = new Test();$client = $test->client;$cilent->method(); //正常

What is the reason for the above code to appear? php5.6 is not a version issue.

Reply content:

class Test{    public $client;        public function __construct($obj)    {        $this->client = new Obj();         //    ......        //  一系列操作    }}// 代码1$client = (new Test())->client;$client->method(); //报错//代码2$test = new Test();$client = $test->client;$cilent->method(); //正常

What is the reason for the above code to appear? php5.6 is not a version issue.

What version of PHP is it?

(new Test($obj))->client

This expression was not supported until 5.4

class Foo {    public function method(){        echo 'hi';    }}class Test {    public $client;    public function __construct($obj){        $this->client = $obj;    }}$obj = new Foo();// 代码1$client = (new Test($obj))->client;$client->method();
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.