锟斤拷锟斤拷锟皆或方凤拷锟侥凤拷锟绞匡拷锟狡o拷锟斤拷通锟斤拷锟斤拷前锟斤拷锟斤拷庸丶锟斤拷锟� public锟斤拷锟斤拷锟叫o拷锟斤拷protected锟斤拷锟杰憋拷锟斤拷锟斤拷锟斤拷 private锟斤拷私锟叫o拷锟斤拷实锟街的★拷锟斤拷锟斤拷锟斤拷为锟斤拷锟叫碉拷锟斤拷锟皆憋拷锟斤拷锟斤拷锟斤拷魏蔚胤锟斤拷锟斤拷锟斤拷省锟斤拷锟斤拷锟斤拷锟轿拷鼙锟斤拷锟斤拷锟斤拷锟斤拷员锟斤拷锟斤拷员锟斤拷锟斤拷锟斤拷锟斤拷约锟斤拷锟斤拷锟斤拷锟酵革拷锟斤拷锟斤拷省锟斤拷锟斤拷锟斤拷锟轿斤拷械锟斤拷锟斤拷员锟斤拷只锟杰憋拷锟戒定锟斤拷锟斤拷锟节碉拷锟斤拷锟斤拷省锟�
锟斤拷锟斤拷锟皆憋拷锟诫定锟斤拷为锟斤拷锟叫o拷锟杰憋拷锟斤拷锟斤拷私锟斤拷之一锟斤拷锟斤拷锟斤拷锟� var 锟斤拷锟藉,锟斤拷锟斤拷为锟斤拷锟叫★拷
Example #1 锟斤拷锟斤拷锟斤拷锟斤拷
<?php
/**
* Define MyClass
*/
class MyClass
{
public $public = 'Public';
protected $protected = 'Protected';
private $private = 'Private';
function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
$obj = new MyClass();
echo $obj->public; // 锟斤拷锟斤拷锟杰憋拷锟斤拷锟斤拷执锟斤拷
echo $obj->protected; // 锟斤拷锟叫伙拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
echo $obj->private; // 锟斤拷锟斤拷也锟斤拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
$obj->printHello(); // 锟斤拷锟� Public锟斤拷Protected 锟斤拷 Private
/**
* Define MyClass2
*/
class MyClass2 extends MyClass
{
// 锟斤拷锟皆讹拷 public 锟斤拷 protected 锟斤拷锟斤拷锟截讹拷锟藉,锟斤拷 private 锟斤拷锟斤拷锟斤拷
protected $protected = 'Protected2';
function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
$obj2 = new MyClass2();
echo $obj2->public; // 锟斤拷锟斤拷锟杰憋拷锟斤拷锟斤拷执锟斤拷
echo $obj2->private; // 未锟斤拷锟斤拷 private
echo $obj2->protected; // 锟斤拷锟叫伙拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
$obj2->printHello(); // 锟斤拷锟� Public锟斤拷Protected2 锟斤拷 Undefined
?>
Note: 为锟剿硷拷锟斤拷锟皆匡拷锟角o拷锟斤拷 PHP 4 锟斤拷使锟斤拷 var 锟截硷拷锟街对憋拷锟斤拷锟斤拷锟叫讹拷锟斤拷姆锟斤拷锟斤拷锟� PHP 5 锟斤拷锟斤拷然锟斤拷效锟斤拷只锟斤拷锟斤拷为 public 锟截硷拷锟街碉拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 PHP 5.1.3 之前锟侥版本锟斤拷锟斤拷锟斤法锟斤拷锟斤拷锟揭伙拷锟�
E_STRICT
锟斤拷锟芥。
锟斤拷锟叫的凤拷锟斤拷锟斤拷锟皆憋拷锟斤拷锟斤拷为锟斤拷锟叫o拷私锟叫伙拷锟杰憋拷锟斤拷锟斤拷锟斤拷锟矫伙拷锟斤拷锟斤拷锟斤拷锟叫╋拷丶锟斤拷郑锟斤拷锟矫凤拷锟斤拷默锟斤拷为锟斤拷锟叫★拷
Example #2 锟斤拷锟斤拷锟斤拷锟斤拷
<?php
/**
* Define MyClass
*/
class MyClass
{
// 锟斤拷锟斤拷一锟斤拷锟斤拷锟叫的癸拷锟届函锟斤拷
public function __construct() { }
// 锟斤拷锟斤拷一锟斤拷锟斤拷锟叫的凤拷锟斤拷
public function MyPublic() { }
// 锟斤拷锟斤拷一锟斤拷锟杰憋拷锟斤拷锟侥凤拷锟斤拷
protected function MyProtected() { }
// 锟斤拷锟斤拷一锟斤拷私锟叫的凤拷锟斤拷
private function MyPrivate() { }
// 锟剿凤拷锟斤拷为锟斤拷锟斤拷
function Foo()
{
$this->MyPublic();
$this->MyProtected();
$this->MyPrivate();
}
}
$myclass = new MyClass;
$myclass->MyPublic(); // 锟斤拷锟斤拷锟杰憋拷锟斤拷锟斤拷执锟斤拷
$myclass->MyProtected(); // 锟斤拷锟叫伙拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
$myclass->MyPrivate(); // 锟斤拷锟叫伙拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
$myclass->Foo(); // 锟斤拷锟叫o拷锟杰憋拷锟斤拷锟斤拷私锟叫讹拷锟斤拷锟斤拷执锟斤拷
/**
* Define MyClass2
*/
class MyClass2 extends MyClass
{
// 锟剿凤拷锟斤拷为锟斤拷锟斤拷
function Foo2()
{
$this->MyPublic();
$this->MyProtected();
$this->MyPrivate(); // 锟斤拷锟叫伙拷锟斤拷锟揭伙拷锟斤拷锟斤拷锟斤拷锟斤拷锟�
}
}
$myclass2 = new MyClass2;
$myclass2->MyPublic(); // 锟斤拷锟斤拷锟杰憋拷锟斤拷锟斤拷执锟斤拷
$myclass2->Foo2(); // 锟斤拷锟叫的猴拷锟杰憋拷锟斤拷锟侥讹拷锟斤拷执锟叫o拷锟斤拷私锟叫的诧拷锟斤拷
class Bar
{
public function test() {
$this->testPrivate();
$this->testPublic();
}
public function testPublic() {
echo "Bar::testPublic\n";
}
private function testPrivate() {
echo "Bar::testPrivate\n";
}
}
class Foo extends Bar
{
public function testPublic() {
echo "Foo::testPublic\n";
}
private function testPrivate() {
echo "Foo::testPrivate\n";
}
}
$myFoo = new foo();
$myFoo->test(); // Bar::testPrivate
// Foo::testPublic
?>
同一锟斤拷锟斤拷亩锟斤拷锟绞癸拷锟斤拷锟酵伙拷锟绞碉拷锟揭诧拷锟斤拷曰锟斤拷锟斤拷锟绞对凤拷锟斤拷私锟斤拷锟斤拷锟杰憋拷锟斤拷锟斤拷员锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷些锟斤拷锟斤拷锟斤拷诓锟斤拷锟斤拷锟绞碉拷值锟较革拷诙锟斤拷锟斤拷锟街拷摹锟�
Example #3 锟斤拷锟斤拷同一锟斤拷锟斤拷锟斤拷锟斤拷锟酵碉拷私锟叫筹拷员
<?php
class Test
{
private $foo;
public function __construct($foo)
{
$this->foo = $foo;
}
private function bar()
{
echo 'Accessed the private method.';
}
public function baz(Test $other)
{
// We can change the private property:
$other->foo = 'hello';
var_dump($other->foo);
// We can also call the private method:
$other->bar();
}
}
$test = new Test('test');
$test->baz(new Test('other'));
?>
锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�
string(5) "hello" Accessed the private method.