PHP/Class/protected
Содержание
Class members" visibility
<source lang="html4strict">
public: can be accessed by any other code private: can be accessed only from within the same class. protected: may be accessed from within the class and from within any class that extends that class.
</source>
Class using access control
<source lang="html4strict">
class Person {
public $name; protected $age; private $salary; public function __construct() { } protected function set_age() { } private function set_salary() { }
}
</source>
Private and protected variables
<source lang="html4strict">
<?php class Staff {
private $name; private $title; protected $wage; protected function clockIn() { echo "Member $this->name clocked in at ".date("h:i:s"); } protected function clockOut() { echo "Member $this->name clocked out at ".date("h:i:s"); }
} ?>
</source>
Properties and methods marked as protected are accessible only through the object that owns them
<source lang="html4strict">
<?
class Dog { public $Name; private function getName( ) { return $this->Name; } } class Poodle extends Dog { public function bark( ) { print ""Woof", says " . $this->getName( ); } } $poppy = new Poodle; $poppy->Name = "Poppy"; $poppy->bark( );
?>
</source>
protected member variable
<source lang="html4strict">
<?php
class Staff { protected $name; protected $title; function __construct() {echo "
Staff constructor called!
";} } class Manager extends Staff { function __construct() { parent::__construct();echo "
Manager constructor called!
";} } $employee = new Manager();
?>
</source>