基礎不好,問個php類調用的初級問題
有個Test.class.php類
name=$name; $this->age=$age; $this->work=$work; $this->do_php(); } public function do_php(){ $content="我的名字是".$this->name.",已經".$this->age."歲了,現在的工作是".$this->work; return $content; } } ?>
調用如下
$content=new \Test('大大','40','php編程');echo $content;
echo 不出內容,$content已經是對象了。
在調用如下
$content=new \Test('大大','40','php編程');echo $content->do_php();
輸出“我的名字是大大,已經40歲了,現在的工作是php編程”內容
請教,怎樣能$content=new \Test('大大','40','php編程');直接echo出內容
回複內容:
基礎不好,問個php類調用的初級問題
有個Test.class.php類
name=$name; $this->age=$age; $this->work=$work; $this->do_php(); } public function do_php(){ $content="我的名字是".$this->name.",已經".$this->age."歲了,現在的工作是".$this->work; return $content; } } ?>
調用如下
$content=new \Test('大大','40','php編程');echo $content;
echo 不出內容,$content已經是對象了。
在調用如下
$content=new \Test('大大','40','php編程');echo $content->do_php();
輸出“我的名字是大大,已經40歲了,現在的工作是php編程”內容
請教,怎樣能$content=new \Test('大大','40','php編程');直接echo出內容
我認為只有 @P醬 一個人的答案是對的 @newborn 是用了其他方式,算不上符合題意,至於那些在建構函式裡面return的就太不靠譜了。
new 用來產生類的執行個體,執行個體的構造方式是調用建構函式,但是建構函式的傳回值是沒有意義的,因為new不是函數,它不會把建構函式的傳回值當做自己的傳回值。
__string是PHP中class可以使用的魔術方法之一,此外還有 __sleep, __get, __set等。而 __string 的含義就是當對象的執行個體需要被轉化為string類型時所要進行的處理。所以,正確的樣本是
class A { public function do() { return 'Your conent'; } public function __string() { return $this->do(); }}$obj = new A();echo $obj; //這個時候其實就觸發了對象的類型轉換,__string()方法被調用。
一定要記得:使用 new 命令擷取對象的執行個體時只有兩個結果,一個是得到對象,另一個是構造失敗產生異常。絕對不會得到一個string的,如果你只想得到一個string,那麼 1) 聲明函數即可 2) 定義常量也行
補充:有些人的答案裡面提出了在函數中直接寫echo語句的方式,千萬不要這麼做,這是壞習慣,防微杜漸。在函數中echo, print, var_dump等都是調試時候用一下而已,最終代碼裡面是不能出現的。因為你無法確定誰會調用你的函數,所以內嵌的echo會給調用者帶來意外的驚喜。
添加__toString
函數
new 出來的對象沒辦法直接返回字串,返回的都是對象;
我大概明白你的意思,你就是想new 的時候直接返回結果了,就少些那個do_php(),可以用靜態方式
方案一:
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;
方案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;
望採納
備忘:重複調用Test類會執行個體化很多個物件在記憶體中,如果需要最佳化,請最佳化g方法
static function g($name, $age, $work) { static $instance; if (!isset($instance)) { $instance = new Test($name, $age, $work); } return $instance; }
我也是小白,這樣你試試?
public function __construct($name,$age,$work){ $this->name=$name; $this->age=$age; $this->work=$work; return $this->do_php(); }
用 var_dump 輸出
你好:
1、樓主第一種做法,使用echo來輸出對象,這是不允許的。echo是一個輸出函數,局限於輸出字串。
報錯如下:
Catchable fatal error: Object of class Test could not be converted to string in /Users/baidu/wwwroot/learn/object.php on line 32
2、第二種做法想法是正確的,但是由於沒有把return結果進行輸出,所以結果為空白。可採用如下2種方案來輸出。(echo放在2個位置均可)
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');
雖然你在__construct的時候調用了do_php
do_php方法也return了字元
但是 __construct裡面調用do_php的時候 ,沒用return
所以改為
......
public function __construct($name,$age,$work){ $this->name=$name; $this->age=$age; $this->work=$work; return $this->do_php(); }
__tostring 直接echo 對象
__condtructor 裡面放echo 直接new一個對象就列印內容
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('大大','40','php編程');
var_dump($content->content);
die();
非常感謝大家的回複!