即使我将 return 更改为 echo,这个类也会给我一个空白输出,我不确定问题是什么,但我显然不精通处理类和对象。
我确定我只是错误地处理了变量/数组,但我看不到在哪里,也许变量不应该在 Class
下声明,因为它们应该只在创建一个人时才返回?我应该在函数中声明变量,还是根本不声明它们,因为它们应该由$args
处理?
更新的问题:如何让它返回每个参数而不仅仅是 FIRSTNAME?
.PHP:
class people_handler
{
public $firstname;
public $middlename;
public $lastname;
public $city;
public $province_state;
/* zip+4 is default for postcode (postal code) */
public $postcode;
public $country;
function create_people($args)
{
$fullname=array($this->firstname,$this->middlename,$this->lastname);
$normname=array($this->firstname,$this->lastname);
$fulladdress=array($this->city,$this->province_state,$this->postcode,$this->country);
if(!$args->middlename&&$args->firstname && $args->lastname && $args->city && $args->province_state && $args->postcode && $args->country)
{
$temp_arr=array($normname,$fulladdress);
foreach($temp_arr as $value)
{
foreach($value as $values)
{
return $values;
}
}
}
else if($args->firstname && $args->middlename && $args->lastname && $args->city && $args->province_state && $args->postcode && $args->country)
{
$temp_arr=array($fullname,$fulladdress);
foreach($temp_arr as $value)
{
foreach($value as $values)
{
return $values;
}
}
}
else
{
die ("Must enter all values excluding middlename.");
}
}
}
$p1=new people_handler;
$p1->firstname="John";
$p1->middlename="Jonah";
$p1->lastname="Jameson";
$p1->city="Lansing";
$p1->province_state="Michigan";
$p1->postcode="48876-4444";
$p1->country="USA";
echo $p1->create_people($p1);
返回:
John
你缺少对象自我引用:到处都是$this
。
每当从类中引用方法或属性时,都需要将$this引用为正在执行该过程的对象的当前实例化。所以,例如...
$fullname=array($firstname,$middlename,$lastname);
成为
$fullname=array($this->firstname,$this->middlename,$this->lastname);
这应该有效,因为您已经为这些属性分配了值。
编辑:进一步查看代码,不断通过循环返回值不会管理对浏览器的回显。您可以echo $value
而不是返回它,也可以从值构建一个数组并返回该数组,并让脚本处理数组以回显到浏览器。
编辑第二个:要获取所有值,您需要在构建它们时收集它们。另一种选择是简单地将它们作为方法的一部分输出到浏览器。这两个选项都有效,但将它们收集到数组中使其更具可移植性,但也需要更多的代码来维护。同样,您不需要将对象传递到自身中即可使方法正常工作。
echo $p1->create_people($p1);
应该是...
$p1->create_people();
在create_people
您将拥有...
function create_people()
{
$fullname=array($this->firstname,$this->middlename,$this->lastname);
$normname=array($this->firstname,$this->lastname);
$fulladdress=array($this->city, $this->province_state, $this->postcode, $this->country);
if($args->firstname && $args->lastname && $args->city && $args->province_state && $args->postcode && $args->country)
{ //Don't bother including middlename if it doesn't matter if it is filled or not...
$temp_arr = array($normname, $fulladdress);
foreach($temp_arr as $value)
{
foreach($value as $values)
{
echo $values;
}
}
} else {
die ("Must enter all values excluding middlename.");
}
}
这应该行得通。
除了自引用问题(顺便说一句,也不需要$args
,因为这应该是自我引用),你的循环结构是错误的。
$temp_arr=array($normname,$fulladdress);
foreach($temp_arr as $value)
{
foreach($value as $values)
{
return $values;
}
}
这将:
- 遍历temp_arr,查找$normname作为第一个值
- 将$normname视为数组并循环遍历它
- 返回它在 $normname 中找到的第一个值
- 函数到此结束,其他所有内容都不执行。
一个函数只能有一个返回值。如果需要返回有关多个事物的信息,则需要将其作为数组或对象返回,以便将其全部包装在一个元素中。
目前我不太确定你想在你的课堂上完成什么,所以很遗憾,我无法帮助你完成你需要做的事情。
编辑:在这种情况下,您无需返回任何内容。您的类使类中的所有函数都可以访问这些变量。使用"new",您可以创建对象的实例,即创建"people_handler"。此people_handler具有有关它的属性,您可以将其公开,因此可以从类外部设置它们(这在更大的项目中可能不是一个好主意,但似乎很好)。作为类一部分的所有函数(即类内部)都可以使用自引用访问这些属性当前对该特定people_handler具有的值,$this
:
class TestClass {
public fullname; //a random "property"
function echoFullname() {
echo $this->fullname; //whatever fullname is at the moment for the TestClass object we are using
}
}
$a = new TestClass(); //Create a TestClass object
$a->fullname = "Alex"; //make its name "Alex"
$b = new TestClass(); //Create another TestClass object
$b->fullname = "Carl"; //but let's name him Carl
$a->echoFullname(); //And now output the names
$b->echoFullname();
显然,这没有实际用途,但希望能说明它是如何工作的。如您所见,变量传递根本不是必需的。
14 行:
$fullname=array($firstname,$middlename,$lastname);
可能应该是:
$fullname=array($this->firstname,$this->middlename,$this->lastname);
同一行 16:
$fulladdress=array($city,$province_state,$postcode,$country);