这是工作代码,但我想知道不使用另一个对象(注释$foo)我如何使用$bar对象使用类Foo的printItem()方法.新的oop编程概念所以可能是一个弱的问题,但真的无法找到:(
我使用范围解析运算符来使用Foo类的printItem(),现在我的查询就是当我们可以使用这个功能时,创建对象有什么用?何时在适当的编码环境中使用范围解析运算符.
- <?PHP
- class Foo
- {
- public function printItem($string)
- {
- echo "This is in class Foo ". $string ."<br />";
- }
- public function printPHP()
- {
- echo "PHP is great "."<br />";
- }
- }
- class Bar extends Foo
- {
- public function printItem($string)
- {
- echo "This is in class Bar ". $string ."<br />";
- }
- }
- //$foo = new Foo;
- $bar = new Bar;
- $bar->printPHP();
- $bar->printItem("Bar class object");
- //Foo::printItem("Mental Case");