gpt4 book ai didi

php - 为什么下面的代码打印出 10 而不是 null?

转载 作者:可可西里 更新时间:2023-11-01 00:00:22 28 4
gpt4 key购买 nike

以下代码输出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();

最佳答案

因为 public function 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

关于php - 为什么下面的代码打印出 10 而不是 null?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21432710/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com