例子:
<html>
<head>
<title></title>
</head>
<body>
<?php
class Book {
private $title;
private $isbn;
private $copies;
public function setIsbn($isbn) {
$this->isbn = $isbn;
}
public function getTitle() {
$this->title = "Beginning Python";
print "Title: ".$this->title."<br />";
}
public function getNumberCopies() {
$this->copies = 5;
print "Number copies available: ".$this->copies."<br />";
}
public function __construct() {
echo "yes";
}
public function __destruct() {
echo "no";
}
}
class CAO {
public function __construct() {
Book::__construct();
}
public function __destruct() {
Book::__destruct();
}
}
$book = new CAO();
?>
</body>
</html>运行结果:
yes
Fatal error: Non-static method Book::__destruct() cannot be called statically, assuming $this from incompatible context in E:\Apache2\Apache2\htdocs\PHPProject\index.php on line 44
<html>
<head>
<title></title>
</head>
<body>
<?php
class Book {
private $title;
private $isbn;
private $copies;
public function setIsbn($isbn) {
$this->isbn = $isbn;
}
public function getTitle() {
$this->title = "Beginning Python";
print "Title: ".$this->title."<br />";
}
public function getNumberCopies() {
$this->copies = 5;
print "Number copies available: ".$this->copies."<br />";
}
public function __construct() {
echo "yes";
}
public function __destruct() {
echo "no";
}
}
class CAO {
public function __construct() {
Book::__construct();
}
public function __destruct() {
Book::__destruct();
}
}
$book = new CAO();
?>
</body>
</html>运行结果:
yes
Fatal error: Non-static method Book::__destruct() cannot be called statically, assuming $this from incompatible context in E:\Apache2\Apache2\htdocs\PHPProject\index.php on line 44
class CAO extends Book {
}
继承,而不是调用静态方法,construct 和 destruct 都是魔术函数,会自动执行,不需要你再去人肉运行
我主要是想研究一下,但为什么construct可以,就destruct出错?
你升级一下php版本看看?呵呵Fatal error: Non-static method Book::__construct() cannot be called statically, assuming $this from incompatible context in
我这个已经是PHP5了喔,还要升级啊?
还是php 想挖点java 的码农弃暗投明?哈哈哈