首页 > 基础不好,问个php类调用的初级问题

基础不好,问个php类调用的初级问题

基础不好,问个php类调用的初级问题

有个Test.class.php类

<?php     
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;
    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出内容


__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();


非常感谢大家的回复!


我也是小白,这样你试试?

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

虽然你在__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();
  }  

new 出来的对象没办法直接返回字符串,返回的都是对象;
我大概明白你的意思,你就是想new 的时候直接返回结果了,就少些那个do_php(),可以用静态的方式

方案一:

<?php

class Test
{

    private $name;
    private $age;
    private $work;

    static function g($name, $age, $work)
    {
        return new Test($name, $age, $work);
    }

    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,'程序猿')->do_php();
echo $c;

方案2

<?php

class Test
{

    private $name;
    private $age;
    private $work;

    static function g($name, $age, $work)
    {
        $instance = new Test($name, $age, $work);
        return $instance->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;
    }

用 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');

我认为只有 @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函数

【热门文章】
【热门文章】