duanqiang2617 2010-08-27 08:25
浏览 70

java和php之间微妙的oop差异[关闭]

I found this case

** php example **

abstract class class1{  
    function test(){}  
}


abstract class class2 extends class1{  
    abstract function test();  
} 

This oop concept works in Java, in PHP it doesn't.(Cannot make non abstract method class1::test() abstract in class class2)

What other subtle differences there are between Java and PHP oop ?

  • 写回答

1条回答 默认 最新

  • doutang3760 2011-10-07 09:02
    关注

    Java and PHP (even when using OO PHP) have a vast array of differences.

    Just a few thoughts off the top of my head:

    1. Java is strongly-typed, PHP is not, although there is a limited scope for type-hinting in PHP. This makes a huge difference to method signatures. In PHP, you can only force method parameters to be of a certain class or interface or an array:

      public function myMethod(SomeClass $foo, array $bar){}

      ...but you cannot type-hint for primitives! So public function myMethod(int $foo, boolean $bar){} is invalid and will throw a parse error.

      Furthermore, any parameter that has been type-hinted cannot be passed as null unless null is given as a default value. So to allow nulls, you need to use:

      public function myMethod(SomeClass $foo = null)

    2. PHP does not require (or even support) specifying the return type of a function.

    3. PHP classes do not have final fields, although what would be a static final field in Java is a const in PHP. EDIT: A const in PHP is more limited than a static final in Java as the latter can be an array or object instance, whereas the former must be a constant value (number or a string, essentially).

    4. "Overloading" in PHP does not mean the same as it does in Java. In Java, it means specifying multiple methods of the same name, but with a different set of parameters:

      public void myMethod(int foo){}; public void myMethod(float foo){};

      In PHP, it refers to the dynamic creation of properties and methods using the __get(), __set() and __callStatic() "magic" methods. See the PHP manual for a description on their use. Java-style method overloading is impossible in PHP and an attempt to redeclare a method (with or without a different set of parameters) will fail.

    5. May be obvious to some, but in PHP you use :: to access static methods and properties and -> to access instance ones, but in Java . is used for both.

    6. PHP doesn't have packages, but it does have namespaces.

    7. As of PHP5, constructors in PHP are not supposed to be methods with a name that matches the class, like in Java, but the magic method __construct() should be declared instead, although the PHP4 style is supported for backward-compatibility. Also, PHP has a destructor method named __destruct().

    8. In Java, all classes inherit from Object, but there is no such generic super-class in PHP.

    9. Even when maximizing the amount of OOP in a PHP script, it still relies on a procedural flow; there's no class-level entry point like in Java (i.e., public static void main(String[] args) or public void init() for applets).

    评论

报告相同问题?

悬赏问题

  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗