Home  >  Article  >  Backend Development  >  经典例子:面向对象版图形计算器

经典例子:面向对象版图形计算器

WBOY
WBOYOriginal
2016-06-23 13:59:491072browse

在index.php文件中有
echo new Form("index.php");    //Form是一个类,里面有构造方法。
      
echo new Result();   
这是什么意思,new不是创建对象吗?这样输出对象也可以啊。还有创建的对象叫什么名字?
附 :
Form类的构造方法如下:
function __construct() {
        $this->action=$action;
        $this->shape=isset($_GET["action"])?$_GET["action"]:"rect";
    }
        


回复讨论(解决方案)

echo new Form("index.php");  输出Form的实例化对象,它是一个对象。用echo 输出是会报错的,要用print_r(); 打印才行。

会不会是同时调用了,Form中的构造方法。。把包含index.php的对象传给  $action 

是的,不过你的构造函数要改为 __construct($action) 才行吧?

额、、是的。那实例化的对象名叫什么啊。

echo new Result(); 
相当于
$p = new Result(); 
echo $p;

这种写法的前提条件是类中定义了 __toString 方法
而 __toString 方法是将这个类的实例转换成一个字符串,当然具体什么内容由你来决定

哦。。。这样啊。。。后面有__toString()方法。。多谢大神

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn