无法将数组作为类的参数传递

时间:2015-09-27 15:38:42

标签: php

我无法将数组作为参数传递给类,我不知道该怎么做。我写这篇文章是因为它告诉我这篇文章主要是代码,我无法发布,所以我需要添加更多细节。

人:

class Human{
    protected $height;
    protected $weight;
    protected $age;
    protected $gender;
    protected $nat;

    public function __construct($height,$weight,$age,$gender,$nat){
        $this->height = $height;
        $this->weight = $weight;
        $this->age = $age;
        $this->gender = $gender;
        $this->nat = $nat;    
    }

    //toString method
    public function __toString(){
        return 
        "height: ".$this->height.
        " meters;<br /> weight: ".$this->weight.
        ";<br /> age: ".$this->age.
        ";<br /> gender: ".$this->gender.
        ";<br /> nationality: ".$this->nat;
    }

    //GET methods
    public function getHeight(){ 
        return $this->height; 
    }
    public function getWeight(){ 
        return $this->weight; 
    }
    public function getAge(){ 
        return $this->age; 
    }
    public function getGender(){ 
        return $this->gender; 
    }
    public function getNat(){ 
        return $this->nat; 
    }

    //SET methods
    public function setHeight($height){ 
        $this->height = $height;
    }
    public function setWeight($weight){ 
        $this->weight = $weight; 
    }
    public function setAge($age){ 
        $this->age = $age; 
    }
    public function setGender($gender){ 
        $this->gender = $gender;
    }
    public function setNationality($nat){ 
        $this->nationality = $nat;
    } 
}

学生:

class Student extends Human{
    private $uni;
    private $year;
    private $faculty; 

    public function __construct($height,$weight,$age,$gender,$nat,$uni,$year,$faculty){
        parent::__construct($height,$weight,$age,$gender,$nat,$uni,$year,$faculty);
        $this->uni = $uni;
        $this->year = $year;
        $this->faculty = $faculty;    
    }  

    public function __toString(){
        return 
        "height: ".$this->height.
        " meters;<br /> weight: ".$this->weight.
        ";<br /> age: ".$this->age.
        ";<br /> gender: ".$this->gender.
        ";<br /> nationality: ".$this->nat.
        ";<br /> university: ".$this->uni.
        ";<br /> year: ".$this->year.
        ";<br /> faculty: ".$this->faculty;

    }

    //GET methods
    public function getUni(){
        return $this->uni;    
    }
    public function getYear(){
        return $this->year;    
    }
    public function getFaculty(){
        return $this->faculty;
    }

    //SET methods
    public function setUni($uni){
        $this->uni = $uni;        
    }
    public function setYear($year){
        $this->year = $year;    
    }
    public function setFaculty($faculty){
        $this->faculty = $faculty;    
    }

    //adding year of studying to student
    public function add($year){
        $this->year = $year;
        $year++; 
        echo $year;;    
    }
}

编程:

class Programmer extends Human{
    public $lang = array("PHP/SQL", "Java", "C#", "HTML/CSS", "Javascript");
    private $exp;
    private $salary;
    private $company;

    public function __construct($height,$weight,$age,$gender,$nat,$lang,$exp,$salary,$company){
        parent::__construct($height,$weight,$age,$gender,$nat,$lang,$exp,$salary,$company);
        $this->lang = $lang;
        $this->exp = $exp;
        $this->salary = $salary;
        $this->company = $company;    
    }  

    public function __toString(){
        return 
        "height: ".$this->height.
        " meters;<br /> weight: ".$this->weight.
        ";<br /> age: ".$this->age.
        ";<br /> gender: ".$this->gender.
        ";<br /> nationality: ".$this->nat.
        ";<br /> languages: ".$this->lang;
        ";<br /> experience: ".$this->exp;
        ";<br /> salary: ".$this->salary;
        ";<br /> company: ".$this->company;

    } 

    //get aarray
    public function getArr($lang){
        for($i = 0; $i < count($lang); $i++){
            echo "element s indeksom $i raven: ".$i."<br />";
            echo $i;    
        }
    }   
}

代码:

$human = new Human(1.75, 80, 22, "male", "australian");
echo $human; echo "<br />";
echo "-------------------<br />";

$student = new Student(1.70, 90, 24, "female", "british", "harvard", 3, "IT");
echo $student; echo "<br />";
echo "-------------------<br />";

echo $student->getYear(); echo "<br />";
echo $student->add(3); echo "<br />";
echo "-------------------<br />";

//i cant pass array as a parameter
$programmer = new Programmer(1.80, 70, 27, "male", "british", 2, 5,10000,"Google");
echo $programmer; echo "<br />";   
echo $programmer->getArr(1);

2 个答案:

答案 0 :(得分:1)

我不太明白你的问题,但我会尝试修复你的代码。

你说你不能将数组传递给函数。您的类中唯一可以使用数组的函数是getArr($lang)

调用getArr(1)时遇到的问题是1被认为是要迭代的数组。但是1不是数组,1只是一个整数。

所以你应该再想一想你在函数getArr中想做什么:

  • 显示数组的所有元素,传递参数
  • 显示带有索引的$lang类属性中的元素,作为参数传递。

<强>更新

好的,如果你希望getArr函数显示预定义类属性$lang中的所有语言,那么你不需要将它作为参数传递,因为类实例已经知道它,所以你可以相应地重写你的功能:

public function getArr() {
    // use $this->lang to access $lang property
    for ($i = 0; $i < count($this->lang); $i++) {
        // access $i element of array with []-notation
        echo "element s indeksom $i raven: " . $this->lang[$i] . "<br />";
    }
} 

另一个更新

你的public function __construct($height,$weight,$age,$gender,$nat,$lang 对于类Programmer$lang个参数。你在其中传递了值2。因此,$this->lang变为2,这也不是数组。你为什么这么做?

答案 1 :(得分:0)

你想这样做吗?

$programmer = new Programmer(1.80, 70, 27, "male", "british", array('HTML/CSS', 'PHP/SQL'), 5, 10000, "Google");