加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php – 为什么以下代码打印出10而不是null?

发布时间:2020-12-13 22:06:11 所属栏目:PHP教程 来源:网络整理
导读:以下代码输出10.为什么? ?php class a{ var $c; public function a(){ $this-c=10; }}class b extends a{ public function print_a(){ print $this-c; }}$b=new b;$b-print_a(); 解决方法 因为公共函数a()是构造函数. For backwards compatibility,if PHP 5
以下代码输出10.为什么?

<?php 

class a{
    var $c;

    public function a(){
        $this->c=10;
    }
}

class b extends a{

    public function print_a(){
        print $this->c;
    }
}

$b=new b;
$b->print_a();

解决方法

因为公共函数a()是构造函数.

For backwards compatibility,if PHP 5 cannot find a __construct() function for a given class,and the class did not inherit one from a parent class,it will search for the old-style constructor function,by the name of the class.

在PHP constructor manual看到更多

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读