Get the App
SLTechnology News&Howtos  ›  Network Security  › 

Access to members in objects and access to private members

Shulou Source: shulou.com Published: 2022-06-01 06:23:32 09月12日 Update

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

? >

Tags: Method Zhang San member private underlined two between time space object Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Xiaomi macOS Redmi vpn