小编Lil*_*aco的帖子

如何重写 PHP 类的构造函数?

这是我的代码。我没有经验,所以请用简单的话告诉我答案。

<?php
function println($message) {
    echo "\n".$message;
}
Run Code Online (Sandbox Code Playgroud)

println 本质上是 echo 但在消息之前有一个 \n 。我只是习惯了Python 3,但不能使用print()。

class Car {
    public function __construct($name) {
    //If the constructor is that of a car, it will be said that a car was made.

        println("Car made!");
        $this->distance_drived = 0;
        $this->name = $name;
    }
    function introductory() {
        return "This car is called ".$this->name.".";
    }
}

class Battery {
    function __construct($max_capacity, $current_capacity) {
        echo "Constructed!";
        $this->max_capacity = $max_capacity;
        $this->current_capacity = $current_capacity;
    }
    function fill($amount) {
        if ($amount …
Run Code Online (Sandbox Code Playgroud)

php oop

0
推荐指数
1
解决办法
9915
查看次数

标签 统计

oop ×1

php ×1