OOP PHP 起始级别 - 它没有得到名称。不显示任何东西
OOP PHP start level - It does not get the name. Does not display anything
我刚开始(现在)尝试学习 OOP PHP,我不敢相信我已经卡住了!
我找不到很多容易理解的教程(我有一个来自 killerphp 的初学者教程,但我想我可能需要较低的级别...),问题是...
教程告诉我创建 2 个文件; index.php 和 class_lib.php:
class_lib.php
<?php
class Person { //we define a class adding class before the name of the class
//Properties of the person class
var $name;
}
index.php
<!DOCTYPE html>
<html>
<head>
<title>Learning OOP PHP</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php include ("class_lib.php"); ?>
</head>
<body>
<?php
$stefan = new Person();
$stefan->set_name("Stefan Grey");
echo "Stefan's full name is: " . $stefan->get_name();
?>
</body>
</html>
有谁能解释一下为什么它不输出 "Stefan's full name is: Stefan Grey" 吗?事实上它没有显示任何东西:/
如果有人知道面向真正傻瓜的 OOP 教程(我需要基本的解释和简单的,我的理解力很差,我总是需要阅读一千遍才能理解它们的意思)。
谢谢!!
作为@先生。前面提到外星人,你的class里没有getter和setter方法。
class_lib.php
<?php
class Person { //we define a class adding class before the name of the class
//Properties of the person class
var $name;
function set_name($new_name)
{
$this->gt_name = $new_name;
}
function get_name()
{
return $this->gt_name;
}
}
?>
index.php
<!DOCTYPE html>
<html>
<head>
<title>Learning OOP PHP</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php include ("class_lib.php"); ?>
</head>
<body>
<?php
$stefan = new Person();
$stefan->set_name("Stefan Grey");
echo "Stefan's full name is: " . $stefan->get_name();
?>
</body>
</html>
<?php
//initializing your class
$stefan = new Person();
//accessing "public function set_name($name)" in class
$stefan->set_name("Stefan Grey");
//accessing "public function get_name()" in class
echo "Stefan's full name is: " . $stefan->get_name();
?>
您正在访问未在 class 中定义的函数 - 也许您应该尝试在 class 中添加这些函数 - 可能像这样:
<?php
class Person {
//we define a class adding class before the name of the class
//Properties of the person class
private $name = "";
//this is a "setter-function", for setting values, that are not accessible from outside - so you have control over what is set
public function set_name ($name)
{
if ($name != "Captain Jack Sparrow") {
$this->name = $name;
}
}
//this is a "getter-function", for getting values from non-public vars - you can also do manipulation here
public function get_name ()
{
if ($this->name != "") { //if $this->name is not "" (Empty String),
return $this->name; //return $this->name to your ->get_name() position
} else { //else (if $this->name is "" (Empty String)
return "Nemo"; //return a standard-value
}
}
}
这就是 "encapsulation" 您正在寻找的 oop-class 概念。
php 有很多语言的很棒的文档 - 可能你会在这里闲逛一段时间:
http://php.net/manual/en/language.oop5.php
我刚开始(现在)尝试学习 OOP PHP,我不敢相信我已经卡住了!
我找不到很多容易理解的教程(我有一个来自 killerphp 的初学者教程,但我想我可能需要较低的级别...),问题是...
教程告诉我创建 2 个文件; index.php 和 class_lib.php:
class_lib.php
<?php
class Person { //we define a class adding class before the name of the class
//Properties of the person class
var $name;
}
index.php
<!DOCTYPE html>
<html>
<head>
<title>Learning OOP PHP</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php include ("class_lib.php"); ?>
</head>
<body>
<?php
$stefan = new Person();
$stefan->set_name("Stefan Grey");
echo "Stefan's full name is: " . $stefan->get_name();
?>
</body>
</html>
有谁能解释一下为什么它不输出 "Stefan's full name is: Stefan Grey" 吗?事实上它没有显示任何东西:/
如果有人知道面向真正傻瓜的 OOP 教程(我需要基本的解释和简单的,我的理解力很差,我总是需要阅读一千遍才能理解它们的意思)。
谢谢!!
作为@先生。前面提到外星人,你的class里没有getter和setter方法。
class_lib.php
<?php
class Person { //we define a class adding class before the name of the class
//Properties of the person class
var $name;
function set_name($new_name)
{
$this->gt_name = $new_name;
}
function get_name()
{
return $this->gt_name;
}
}
?>
index.php
<!DOCTYPE html>
<html>
<head>
<title>Learning OOP PHP</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<?php include ("class_lib.php"); ?>
</head>
<body>
<?php
$stefan = new Person();
$stefan->set_name("Stefan Grey");
echo "Stefan's full name is: " . $stefan->get_name();
?>
</body>
</html>
<?php
//initializing your class
$stefan = new Person();
//accessing "public function set_name($name)" in class
$stefan->set_name("Stefan Grey");
//accessing "public function get_name()" in class
echo "Stefan's full name is: " . $stefan->get_name();
?>
您正在访问未在 class 中定义的函数 - 也许您应该尝试在 class 中添加这些函数 - 可能像这样:
<?php
class Person {
//we define a class adding class before the name of the class
//Properties of the person class
private $name = "";
//this is a "setter-function", for setting values, that are not accessible from outside - so you have control over what is set
public function set_name ($name)
{
if ($name != "Captain Jack Sparrow") {
$this->name = $name;
}
}
//this is a "getter-function", for getting values from non-public vars - you can also do manipulation here
public function get_name ()
{
if ($this->name != "") { //if $this->name is not "" (Empty String),
return $this->name; //return $this->name to your ->get_name() position
} else { //else (if $this->name is "" (Empty String)
return "Nemo"; //return a standard-value
}
}
}
这就是 "encapsulation" 您正在寻找的 oop-class 概念。
php 有很多语言的很棒的文档 - 可能你会在这里闲逛一段时间: http://php.net/manual/en/language.oop5.php