Access to members in objects and access to private members
Method 1:
Class Person {
Var $name
Var $sex
Var $age
Function say () {
Echo "this person is talking"; / / if echo is not written here, write return "this person is talking"; and when calling, write echo $person1- > say
}
Function run () {
Echo, "this man is walking."
}
}
$person1=new person ()
$person2=new person ()
$person1- > name= "Zhang San"
$person1- > sex= "female"
Echo $person1- > name
Echo ""
Echo $person1- > sex
$person1- > say (); / / because echo has already been written in function, it is directly referenced here (that is, $person1- > say ();)
Method 2: (access by private members)
Class Person {
Private $name
Private $sex
Private $age
Public function _ _ set ($name,$value) {/ / function is followed by two underscores with a space between them
Return $this- > $name=$value
}
Public function _ _ get ($name) {
Return $this- > $name
}
}
$person1= new person ()
$person2= new person ()
$person1- > name= "Zhang San"
Echo $person1- > name; / / if you have already written echo in function, just write $person1- > name here
$person1- > sex= "40"
Echo $person1- > sex
? >