OOPに切り替えようとしています。インターネット上で killerphp によって書かれた便利な PDF を見つけました。エラーが発生したため、今まで彼の例に従いました。出力は次のとおりです。
警告: person::__construct() の引数 1 がありません。15 行目で C:\xampp\htdocs\oop\index.php で呼び出され、8 行目で C:\xampp\htdocs\oop\class_lib.php で定義されています。
と
注意: 未定義の変数: C:\xampp\htdocs\oop\class_lib.php の 10 行目の person_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
?>
コメントは気にしないでください。私はそれらを学習に使用します。ありがとうございます。評価を下げる前に質問を編集する必要がある場合はお知らせください。乾杯!