phpオブジェクト向けダイナミックバインドと静的バインドの理解について
- /* php */
-
- // 1, public protected private
-
- // eg: private
- class User
- {
- private $name = "wxp";
- private function setName($name)
- {
- if(isset($this->name))
- {
- $this->name = $name;
- }
- }
- }
- class sUser extends User
- {
-
- }
- $sUser = new sUser();
- // print_r($sUser->name); // : private
-
- // 2,
- // , : , , , 。
- // private
-
- class Users
- {
- protected function isPassed()
- {
- return false;
- }
- public function delUsers()
- {
- if(!$this->isPassed())
- {
- print('error');
- return false;
- }
- else
- {
- print("ok");
- }
- }
- }
- class PassUsers extends Users
- {
- protected function isPassed()
- {
- return true;
- }
- }
-
- $users = new Users();
- $passusers = new PassUsers();
- // $users->delUsers(); //echo :error
- // $passusers->delUsers(); //echo : ok
-
- // 3
- // private , , , , isPassed 。
-
- class Users_
- {
- private function isPassed()
- {
- return false;
- }
- public function delUsers()
- {
- if(!$this->isPassed())
- {
- print('error');
- return false;
- }
- else
- {
- print("ok");
- }
- }
- }
- class PassUsers_ extends Users_
- {
- private function isPassed()
- {
- return true;
- }
- }
- $users_ = new Users_();
- $passusers_ = new PassUsers_();
- $users_->delUsers(); //echo :error
- $passusers_->delUsers(); //echo : error