博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
吴裕雄--天生自然 PHP开发学习:面向对象
阅读量:4580 次
发布时间:2019-06-09

本文共 4851 字,大约阅读时间需要 16 分钟。

url = $par; } function getUrl(){ echo $this->url . PHP_EOL; } function setTitle($par){ $this->title = $par; } function getTitle(){ echo $this->title . PHP_EOL; }}?>
$runoob = new Site;$taobao = new Site;$google = new Site;
// 调用成员函数,设置标题和URL$runoob->setTitle( "菜鸟教程" );$taobao->setTitle( "淘宝" );$google->setTitle( "Google 搜索" );$runoob->setUrl( 'www.runoob.com' );$taobao->setUrl( 'www.taobao.com' );$google->setUrl( 'www.google.com' );// 调用成员函数,获取标题和URL$runoob->getTitle();$taobao->getTitle();$google->getTitle();$runoob->getUrl();$taobao->getUrl();$google->getUrl();
url = $par; } function getUrl(){ echo $this->url . PHP_EOL; } function setTitle($par){ $this->title = $par; } function getTitle(){ echo $this->title . PHP_EOL; } } $runoob = new Site; $taobao = new Site; $google = new Site; // 调用成员函数,设置标题和URL $runoob->setTitle( "菜鸟教程" ); $taobao->setTitle( "淘宝" ); $google->setTitle( "Google 搜索" ); $runoob->setUrl( 'www.runoob.com' ); $taobao->setUrl( 'www.taobao.com' ); $google->setUrl( 'www.google.com' ); // 调用成员函数,获取标题和URL $runoob->getTitle(); $taobao->getTitle(); $google->getTitle(); $runoob->getUrl(); $taobao->getUrl(); $google->getUrl(); ?>

function __construct( $par1, $par2 ) {   $this->url = $par1;   $this->title = $par2;}
$runoob = new Site('www.runoob.com', '菜鸟教程'); $taobao = new Site('www.taobao.com', '淘宝'); $google = new Site('www.google.com', 'Google 搜索'); // 调用成员函数,获取标题和URL $runoob->getTitle(); $taobao->getTitle(); $google->getTitle(); $runoob->getUrl(); $taobao->getUrl(); $google->getUrl();
name = "MyDestructableClass"; } function __destruct() { print "销毁 " . $this->name . "\n"; }}$obj = new MyDestructableClass();?>

category = $par; } function getCate(){ echo $this->category . PHP_EOL; }}
function getUrl() {   echo $this->url . PHP_EOL;   return $this->url;}   function getTitle(){   echo $this->title . PHP_EOL;   return $this->title;}
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; // 未定义 privateecho $obj2->protected; // 这行会产生一个致命错误$obj2->printHello(); // 输出 Public、Protected2 和 Undefined?>
MyPublic(); $this->MyProtected(); $this->MyPrivate(); }}$myclass = new MyClass;$myclass->MyPublic(); // 这行能被正常执行$myclass->MyProtected(); // 这行会产生一个致命错误$myclass->MyPrivate(); // 这行会产生一个致命错误$myclass->Foo(); // 公有,受保护,私有都可以执行/** * Define MyClass2 */class MyClass2 extends MyClass{ // 此方法为公有 function Foo2() { $this->MyPublic(); $this->MyProtected(); $this->MyPrivate(); // 这行会产生一个致命错误 }}$myclass2 = new MyClass2;$myclass2->MyPublic(); // 这行能被正常执行$myclass2->Foo2(); // 公有的和受保护的都可执行,但私有的不行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?>
vars[$name] = $var; } public function getHtml($template) { foreach($this->vars as $name => $value) { $template = str_replace('{' . $name . '}', $value, $template); } return $template; }}
showConstant();echo $class::constant . PHP_EOL; // 自 PHP 5.3.0 起?>
getValue() . PHP_EOL; }}class ConcreteClass1 extends AbstractClass{ protected function getValue() { return "ConcreteClass1"; } public function prefixValue($prefix) { return "{
$prefix}ConcreteClass1"; }}class ConcreteClass2 extends AbstractClass{ public function getValue() { return "ConcreteClass2"; } public function prefixValue($prefix) { return "{
$prefix}ConcreteClass2"; }}$class1 = new ConcreteClass1;$class1->printOut();echo $class1->prefixValue('FOO_') . PHP_EOL;$class2 = new ConcreteClass2;$class2->printOut();echo $class2->prefixValue('FOO_') . PHP_EOL;?>

prefixName("Pacman"), "\n";echo $class->prefixName("Pacwoman"), "\n";?>

staticValue() . PHP_EOL;?>

 

转载于:https://www.cnblogs.com/tszr/p/10947797.html

你可能感兴趣的文章