Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Java and PHP (even when using OO PHP) have a vast array of differences.</p> <p>Just a few thoughts off the top of my head:</p> <ol> <li><p>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:</p> <p><code>public function myMethod(SomeClass $foo, array $bar){}</code></p> <p>...but you cannot type-hint for primitives! So <code>public function myMethod(int $foo, boolean $bar){}</code> is invalid and will throw a parse error.</p> <p>Furthermore, any parameter that has been type-hinted cannot be passed as <code>null</code> <em>unless <code>null</code> is given as a default value</em>. So to allow <code>null</code>s, you need to use:</p> <p><code>public function myMethod(SomeClass $foo = null)</code></p></li> <li><p>PHP does not require (or even support) specifying the return type of a function.</p></li> <li><p>PHP classes do not have <code>final</code> fields, although what would be a <code>static final</code> field in Java is a <code>const</code> in PHP. EDIT: A <code>const</code> in PHP is more limited than a <code>static final</code> 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).</p></li> <li><p>"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:</p> <p><code>public void myMethod(int foo){}; public void myMethod(float foo){};</code></p> <p>In PHP, it refers to the dynamic creation of properties and methods using the <code>__get()</code>, <code>__set()</code> and <code>__callStatic()</code> "magic" methods. See the <a href="http://uk3.php.net/manual/en/language.oop5.overloading.php" rel="noreferrer">PHP manual</a> 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.</p></li> <li><p>May be obvious to some, but in PHP you use <code>::</code> to access static methods and properties and <code>-&gt;</code> to access instance ones, but in Java <code>.</code> is used for both.</p></li> <li><p>PHP doesn't have packages, but it does have <em>namespaces</em>.</p></li> <li><p>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 <code>__construct()</code> should be declared instead, although the PHP4 style is supported for backward-compatibility. Also, PHP has a destructor method named <code>__destruct()</code>.</p></li> <li><p>In Java, all classes inherit from <code>Object</code>, but there is no such generic super-class in PHP.</p></li> <li><p>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., <code>public static void main(String[] args)</code> or <code>public void init()</code> for applets).</p></li> </ol>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload