我正在尝试切换到 OOP。我在网上找到了一份由killerphp 编写的pdf,看起来很有用。一直跟着他的例子直到现在,因为我遇到了一个错误。输出是这样的:
警告:person::__construct() 缺少参数 1,在第 15 行的 C:\xampp\htdocs\oop\index.php 中调用并在第 8 行的 C:\xampp\htdocs\oop\class_lib.php 中定义
和
注意:未定义变量:第 10 行 C:\xampp\htdocs\oop\class_lib.php 中的 people_name
Stefan's full name: Stefan Mischook
Nick's full name: Nick Waddles
这是 index.php(我运行的页面):
<?php
    require_once('class_lib.php');
?>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>OOP in PHP</title>
</head>
    <body>
        <?php
            // Create object without constructor by calling a method
            $stefan = new person();
            $stefan->set_name("Stefan Mischook");
            echo "Stefan's full name: " . $stefan->get_name();
            echo "<br>";
            // Create object with constructor
            $jimmy = new person("Nick Waddles");
            echo "Nick's full name: " . $jimmy->get_name();
        ?>
    </body>
</html>
这是课程:
<?php
    // A variable inside a class is called a "property"
    // Functions inside a class are called "methods"
    class person
    {
        var $name;
        function __construct($persons_name)
        {
            $this->name = $persons_name;
        }
        function set_name($new_name)
        {
            $this->name = $new_name;
        }
        function get_name()
        {
            return $this->name;
        }
    }
    // $this can be considered a special OO PHP keyword
    // A class is NOT an object. The object gets created when you create an instance of the class.
    // To create an object out of a class you need to use the "new" keyword.
    // When accesign methods and properties of a class you use the -> operator.
    // A constructor is a built-in method that allows you to give your properties values when you create an object
?>
没关系评论,我用它们来学习。非常感谢,如果我需要在降级之前编辑我的问题,请告诉我。干杯!