不能将array作为类的参数传递


Can't pass array as a parameter of a class

我不能将数组作为参数传递给类,而且我不知道如何做到这一点。我写这个是因为它告诉我,这篇文章主要是代码,我不能发布它,所以我需要添加更多的细节。

人类:

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);

我不太明白你的问题,但我会尝试用你的代码修复一些东西。

你说你不能传递数组给函数。在你的类中唯一可以处理数组的函数是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 />";
    }
} 
另一个<<p> strong> :

public function __construct($height,$weight,$age,$gender,$nat,$langfor类Programmer$lang参数。然后传入值2。所以,$this->lang变成了2,它也不是一个数组。你为什么要这么做?

你想这么做吗?

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