2013-05-15 2 views
0

누군가 내가 왜 그랬는지 설명해 주시겠습니까? print_r($student->getStudents()) 배열의 크기 만 가져오고 is_array은 false를 반환합니다.정의 된 배열이 false를 반환하는 이유는 무엇입니까?

이 그것이 바로 당신이 그것을 전달하는 배열을 수정하고 배열의 새 길이를 반환, 새로운 배열을 반환하지 않습니다 내 출력

Jane Doe enrolled at Aviation High School 
All students: 
Nope!3 

<?php 
class Student{ 
public $name; 
public $students = array('Jason', 'Joe'); 
public function __construct($name){ 
    $this->name = $name; 
    $this->students = array_push($this->students, $name); 
} 
public function lastName(){ 
    return "Doe"; 
} 

public function getStudents(){ 
    return $this->students; 
} 
} 

class School{ 
public $name; 
public function __construct($name){ 
    $this->name = $name; 
} 
} 

class Admin{ 
public function enroll(Student $student,School $school){ 
    echo $student->name.' '.$student->lastName().' enrolled at '. $school->name; 
    echo '<br />All students:<br />'; 
    echo is_array($student->getStudents()) ? 'Yeah!':'Nope!' ; 
} 
} 

$student = new Student("Jane"); 
$school = new School("Aviation High School"); 

$admin = new Admin(); 
$admin->enroll($student, $school); 
+3

[array_push (http://php.net/manual/en/function.array-push.php) 내가 말할 것입니다 당신이 그것을 사용하는 방법을 작동하지 않습니다. – cheesemacfly

답변

5

하면 배열을 덮어 쓰기 때문에.

http://us2.php.net/manual/en/function.array-push.php

변경 :

$this->students = array_push($this->students, $name); 

사람 :

array_push($this->students, $name); 

은 또한 단지 할 : 당신은 학생들을 설정하는

$this->students[] = $name; 
+0

하하, 4 초 만에 너를 패 : 나 +1! –

+0

네 말이 맞아. 'array_push' 함수를 여러 번 사용 했더라도 가끔씩 작은 것들을 간과 할 수 있습니다. – u54r

4

array_push입니다.

따라서 $this->students = array_push($this->students,$name)을 호출 한 후에는 값이 3인데 분명히 배열이 아닙니다. 당신이 배열의 길이 array_push을 수행 할 때

Documentation

관련 문제