-1

我正在关注我在互联网上找到的一个简单的 OOP 教程。我快完成了,但我收到了我在主题中提到的错误。

这是输出:

Stefan's full name: Stefan Mischook

Notice: Use of undefined constant name - assumed 'name' in C:\xampp\htdocs\oop\class_lib.php on line 21

---> JOHNNY FINGERS

这里是index.php:

<?php
    require_once('class_lib.php');
?>
<!DOCTYPE html>
<html>    
<head>
    <meta charset="UTF-8">
    <title>OOP in PHP</title>
</head>    
    <body>
        <?php
            $stefan = new person("Stefan Mischook");
            echo "Stefan's full name: " . $stefan->get_name();

            echo "<br>";

            $james = new employee("Johnny Fingers");
            echo "---> " . $james->get_name();
        ?>
    </body>
</html>

这是 class_lib.php,包含类的文件

<?php
    // A variable inside a class is called a "property"
    // Functions inside a class are called "methods"
    class person
    {
        var $name; // If a property is declared with the "var" keywords it is considered public.

        public $height;

        protected $social_insurance;

        private $pinn_number;

        function __construct($persons_name)
        {
            $this->name = $persons_name;
        }

        protected function set_name($new_name)
        {
            if (name !="Jimmy Two Guns")
            {
                $this->name = strtoupper($new_name);
            }
        }

        public function get_name()
        {
            return $this->name;
        }

        private function get_pinn_number()
        {
            return $this->$pinn_number;
        }
    }

    class employee extends person
    {

        protected function set_name($new_name)
        {
            if ($new_name == "Stefan Lamp")
            {
                $this->name = $new_name;
            }
            else if ($new_name == "Johnny Fingers")
            {
                person::set_name($new_name);
            }
        }

        function __construct($employee_name)
        {
            $this->set_name($employee_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.

    // A handle is...

    // To create an object out of a class you need to use the "new" keyword.

    // When accessing 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

    // A private property can be accessed only by the same class.

    // A protected property can be accessed by the same class and classes that are derived from that class.

    // A public property has no access restrictions.

    // Public, private and protected are called "acces modifiers" and it's all about control. It makes sense to control how people use classes.

    // "extends" is the keyword that enables inheritance

    // Child class methods can overwrite parent class methods simply by declaring a method with the same name again.

    /* "::" allows you to specifically name the class where you want PHP to search for a method. If you want to access the method of the parent class instead of the overwritten one of the child class you can do it much simpler by calling it this way: parent::method() (you don't have to type the name of the class).*/

?>

不要介意评论,我将它们用于学习目的。非常感谢您,如果我需要在降级之前编辑我的问题,请告诉我。干杯!

4

3 回答 3

3

这是一个错字。你忘$this->了第 21 行。

你写了

if (name !="Jimmy Two Guns")

但应该是

if ($this->name !="Jimmy Two Guns")
于 2013-11-07T09:17:05.477 回答
1
protected function set_name($new_name)
        {
            if ($this->name !="Jimmy Two Guns")
            {
                $this->name = strtoupper($new_name);
            }
        }
于 2013-11-07T09:18:06.513 回答
1
<?php
// A variable inside a class is called a "property"
// Functions inside a class are called "methods"
class person
{
    var $name; // If a property is declared with the "var" keywords it is considered public.

    public $height;

    protected $social_insurance;

    private $pinn_number;

    function __construct($persons_name)
    {
        $this->name = $persons_name;
    }

    protected function set_name($new_name)
    {
        if ($new_name !="Jimmy Two Guns")
        {
            $this->name = strtoupper($new_name);
        }
    }

    public function get_name()
    {
        return $this->name;
    }

    private function get_pinn_number()
    {
        return $this->$pinn_number;
    }
}

class employee extends person
{

    protected function set_name($new_name)
    {
        if ($new_name == "Stefan Lamp")
        {
            $this->name = $new_name;
        }
        else if ($new_name == "Johnny Fingers")
        {
            person::set_name($new_name);
        }
    }

    function __construct($employee_name)
    {
        $this->set_name($employee_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.

// A handle is...

// To create an object out of a class you need to use the "new" keyword.

// When accessing 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

// A private property can be accessed only by the same class.

// A protected property can be accessed by the same class and classes that are derived from that class.

// A public property has no access restrictions.

// Public, private and protected are called "acces modifiers" and it's all about control. It makes sense to control how people use classes.

// "extends" is the keyword that enables inheritance

// Child class methods can overwrite parent class methods simply by declaring a method with the same name again.
?>
于 2013-11-07T09:21:37.127 回答