使用未定义的常量名称

时间:2013-11-07 09:15:55

标签: php

我正在按照我在互联网上找到的一个简单的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).*/

?>

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

3 个答案:

答案 0 :(得分:3)

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

你写了

if (name !="Jimmy Two Guns")

但它应该是

if ($this->name !="Jimmy Two Guns")

答案 1 :(得分:1)

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

答案 2 :(得分: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.
?>