Base is not good, ask a PHP class call the primary problem

Source: Internet
Author: User
Base is not good, ask a PHP class call the primary problem

There's a Test.class.php class.


  
   name=$name;    $this->age=$age;    $this->work=$work;    $this->do_php();  }        public function do_php(){         $content="我的名字是".$this->name.",已经".$this->age."岁了,现在的工作是".$this->work;    return $content;  }         }     ?>  

Call the following

$content=new \Test('大大','40','php编程');echo $content;

Echo is not content, $content is already an object.

In the following call

$content=new \Test('大大','40','php编程');echo $content->do_php();

Output "My name is big, already 40 years old, now the work is PHP programming" content

Ask, how can $content=new \test (' Big ', ' Max ', ' PHP programming '); directly echo out the content

Reply content:

Base is not good, ask a PHP class call the primary problem

There's a Test.class.php class.


  
   name=$name;    $this->age=$age;    $this->work=$work;    $this->do_php();  }        public function do_php(){         $content="我的名字是".$this->name.",已经".$this->age."岁了,现在的工作是".$this->work;    return $content;  }         }     ?>  

Call the following

$content=new \Test('大大','40','php编程');echo $content;

Echo is not content, $content is already an object.

In the following call

$content=new \Test('大大','40','php编程');echo $content->do_php();

Output "My name is big, already 40 years old, now the work is PHP programming" content

Ask, how can $content=new \test (' Big ', ' Max ', ' PHP programming '); directly echo out the content

I think that only @p sauce One's answer is right @newborn is in the other way, not in line with test instructions, as for those in the constructor return is too unreliable.
New is used to generate an instance of the class, and the instance is constructed by calling the constructor, but the return value of the constructor is meaningless, because new is not a function, and it does not take the return value of the constructor as its own return value.
__string is one of the magic methods that class can use in PHP, in addition to __sleep, __get, __set, and so on. The meaning of __string is the processing to be done when an instance of an object needs to be converted to a string type. So, the correct example is

class A {    public function do() {        return 'Your conent';    }    public function __string() {        return $this->do();    }}$obj = new A();echo $obj; //这个时候其实就触发了对象的类型转换,__string()方法被调用。

Be sure to remember that when you use the new command to get an instance of an object, there are only two results, one is to get the object, and the other is an exception to the construction failure. Never get a string if you want only one string, then 1) declare the function 2) define the constant also line

Add: Some people's answers put forward in the function to write the Echo statement directly, do not do so, this is a bad habit, preventive measure. In the function of Echo, print, var_dump, etc. are debugging time to use it, the final code can not appear. Because you cannot be sure who will call your function, the embedded echo will surprise the caller with unexpected surprises.

Add a __toString function

New objects are not able to return the string directly, the object is returned;
I probably understand what you mean, you just want to return the result when new, less that do_php (), can be used in a static way

Programme one:


  
   name=$name;        $this->age=$age;        $this->work=$work;    }    public function do_php()    {        $content = "我的名字是" . $this->name . ",已经" . $this->age . "岁了,现在的工作是" . $this->work;        return $content;    }}$c = Test::g('张三',42,'程序猿')->do_php();echo $c;

Scenario 2


  
   do_php();    }    public function __construct($name,$age,$work){        $this->name=$name;        $this->age=$age;        $this->work=$work;    }    public function do_php()    {        $content = "我的名字是" . $this->name . ",已经" . $this->age . "岁了,现在的工作是" . $this->work;        return $content;    }}$c = Test::g('张三',42,'程序猿');echo $c;

Hope to adopt

Note: Repeated calls to the test class will instantiate many objects in memory and, if desired, optimize the G method

static function g($name, $age, $work)    {        static $instance;        if (!isset($instance)) {            $instance = new Test($name, $age, $work);        }        return $instance;    }

I'm a little white too, so you try?

  public function __construct($name,$age,$work){    $this->name=$name;    $this->age=$age;    $this->work=$work;    return $this->do_php();  }  

Output with Var_dump

How are you doing:
1, the landlord first approach, using echo to output objects, which is not allowed. Echo is an output function that is limited to the output string.
The error is as follows:
Catchable fatal error:object of class Test could not being converted to string in/users/baidu/wwwroot/learn/object.php on L INE 32

2, the second idea is correct, but because the return result is not output, so the result is empty. The following 2 options can be used for output. (Echo can be placed in 2 positions)

class Test{    private $name;    private $age;    private $work;    public function __construct($name,$age,$work){        $this->name=$name;        $this->age=$age;        $this->work=$work;        $this->do_php();    }    public function do_php(){        $content="我的名字是".$this->name.",已经".$this->age."岁了,现在的工作是".$this->work;        echo  $content;    }}$test = new \Test('luboot','40','php search');
class Test{    private $name;    private $age;    private $work;    public function __construct($name,$age,$work){        $this->name=$name;        $this->age=$age;        $this->work=$work;        echo $this->do_php();    }    public function do_php(){        $content="我的名字是".$this->name.",已经".$this->age."岁了,现在的工作是".$this->work;        return $content;    }}$test = new \Test('luboot','40','php search');

Although you called do_php when you were __construct.

The Do_php method also return the character
But when __construct calls do_php, it doesn't return.
So instead
......

  public function __construct($name,$age,$work){    $this->name=$name;    $this->age=$age;    $this->work=$work;    return $this->do_php();  }  

__tostring Direct Echo Object

__condtructor inside, Echo directly new object to print content

Class test{

Private $name;
Private $age;
Private $work;

Public function __construct ($name, $age, $work) {

$this->name=$name;$this->age=$age;$this->work=$work;$this->content = $this->do_php();

}

Public Function do_php () {

$content="我的名字是".$this->name.",已经".$this->age."岁了,现在的工作是".$this->work;return $content;

}

}
$content =new \test (' Big ', ' Max ', ' PHP programming ');
Var_dump ($content->content);
Die ();

Thank you very much for your reply!

  • 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.