dsjfrkvn818747 2012-04-26 17:22
浏览 5
已采纳

为什么我的班级只返回第一个值?

This Class gives me a blank output even if I change return to echo, I'm not sure what the issue is but I'm obviously not that versed in dealing with Classes and Objects.

I'm sure I'm just handling the variables/arrays incorrectly, but I can't see where, maybe the variables shouldn't be declared under Class since they should only be returned if a person is created? Should I declare variables in the function, or not declare them at all since they should be handled by $args?

Updated Question: How do I get it to return every argument not just 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);

Returns:

John
  • 写回答

3条回答 默认 最新

  • drtoaamk20278 2012-04-26 17:26
    关注

    You're missing the Object self-reference: $this all over the place.

    Anytime you refer to a method or property from within the class, you need to refer to $this as the current instantiation of the Object that is doing the process. So, for instance...

    $fullname=array($firstname,$middlename,$lastname);  
    

    becomes

     $fullname=array($this->firstname,$this->middlename,$this->lastname);  
    

    Which should work, since you assigned the values to those properties already.

    EDIT: Looking at the code further, constantly returning a value through loops won't manage the echoing to the browser. You can either echo $value instead of returning it, or build an array from the values and return that and have the script handle the array to echo to the browser.

    EDIT THE SECOND: To get all the values out, you need to collect them as you build them. Another option is to simply output them to the browser as part of the method. Both options work, but collecting them into an array makes it more portable, but also a fair bit more code to maintain. As well, you do not need to pass the object into itself to get the method to work.

    echo $p1->create_people($p1);
    

    Should be...

    $p1->create_people();
    

    In create_people you'll have...

    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.");
        }
    }
    

    That should work.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计