Php Empty()函数不使用';不能在OOP中工作
我有一段代码,试图检查$name是否为空。在过程风格中,一切都很好,但在OOP中,空函数似乎不起作用。我对PHP不是很有经验,所以请解释问题出在哪里以及如何解决这个问题Php Empty()函数不使用';不能在OOP中工作,php,Php,我有一段代码,试图检查$name是否为空。在过程风格中,一切都很好,但在OOP中,空函数似乎不起作用。我对PHP不是很有经验,所以请解释问题出在哪里以及如何解决这个问题 <?php // The code below creates the class class Check { // Creating some properties (variables tied to an object) public $name; p
<?php
// The code below creates the class
class Check {
// Creating some properties (variables tied to an object)
public $name;
public $subject;
// Assigning the values
public function __construct($name, $subject) {
$this->name=$name;
$this->subject=$subject;
}
// Creating a method (function tied to an object)
public function checking() {
if (empty($name)) {
echo "empty name";
}
else {
echo "name set";
}
}
}
$me = new Check('Mark','somesubject');
echo $me->checking();
?>
您应该使用:
if (empty($this->name)) {
应该是
if (empty($this->name)) {
echo "empty name";
}
else {
echo "name set";
}
因为您在代码中使用的是OOPs概念。当需要引用同一类的变量时,需要使用$this->
关键字,后跟
类的变量名,该变量名在构造函数中声明为
public function __construct($name, $subject) {
$this->name=$name;
$this->subject=$subject;
}
它是一个对象属性,不是一个局部范围的变量,因此
if(empty($this->name)){
阅读