의 사용해야합니다
$this->name
이 클래스에 의해 정의와 객체 내에 전 세계적으로 액세스 할 수 있습니다 개체의 속성입니다. 클래스 외부에서
class myClass{
private $name = "record.log";
function myMethod(){
$name = 'this exists only in the method myMethod()';
$this->name; // this contains the 'record.log' string
}
}
당신이 내에서 정의 된 변수 $name
에 액세스 할 수 없습니다 :
$name
은 아주 간단하게 (함수)
클래스 메소드 내에서 사용되는 변수 만 객체 메소드 내에서 로컬 액세스 목적.
당신은 클래스에 있지만 개체 이름을 사용하여 호출해야합니다 물체의 외부에서 정의 된 속성 $ 이름에 액세스 할 수 있습니다 : 당신이 직접 액세스 할 수 있도록 비공개로 객체 속성을 정의하지만
$obj = new myClass();
$log_file = $obj->name; // this would contain the string 'record.log'
개체의 외부에서 제한됩니다. 액세스 할 수 있으려면 getter/setter를 private 속성에 대한 읽기/쓰기를 처리하는 메서드로 정의해야합니다.
// add to the class methods
public function getName(){
return $this->name;
}
public function setName($value){
// do some validation of the value first
//...
// then assign the value
$this->name = $value;
}
그래서 지금 당신이 문을 사용하여 객체의 외부에서 객체 속성 $ 이름에 액세스 할 수 있습니다
echo $obj->getName(); // prints record.log
$obj->setName('new.log');
echo $obj->getName(); // prints new.log
출처
2017-12-15 13:10:26
ino
을 http://php.net/manual/en/language.oop5.basic.php –
그게 아니라 클래스 –
$ 이름은 함수의 범위 내에서 변수를 검색합니다. 클래스의 함수 밖에서 정의 된 변수를 사용하려면 $ this-> name을 사용하여 액세스해야합니다. –