Я следую простому учебному пособию по ООП, который я нашел в Интернете. Я почти закончен, но получаю ошибку, которую я упоминаю в теме.Использование неопределенного имени константы
Вот результат:
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).*/
?>
Не возражаете комментарии, я использую их для обучения цели. Большое спасибо, и, пожалуйста, дайте мне знать, если мне нужно изменить свой вопрос до рейтинга. Ура!
появляется этот вопрос, чтобы быть вне темы, потому что отладка кода будет не будут полезны для будущих посетителей. –
Это правда. Однако отладка не в моем коде, это код учебника killerphp. Это может помочь нескольким людям после его учебников. Благодаря! :) – SporeDev
@SporeDev вы можете уведомить об этом ошибке в своем учебнике, комментируя (если он в Интернете), это может помочь другим, которые следуют за этим :) – nu6A