PHP basic syntax and the difference from Java

Source: Internet
Author: User

. Represents the addition of strings

In Java.

$ as a prefix for variables, in addition to the variable name definition rules with Java

Parameter passing and method return simultaneous references need to be prefixed &

Example code:
function f (& $a) {}//method parameter value passed in as a reference
Functioin & F () {}//method return value returned as a reference

The reflection calling method in Java corresponds to a mutable function in PHP: var a= "a"; A ();

PHP is a bit more powerful than Java when defining variables, and you can define mutable variables, meaning that the variable name itself can be used as a variable when defining a variable.

Example code:
var a= "a"; var a= "Usea";//A is a variable that uses the value of a as a variable name, or var {a}= "use a";

PHP classes must be loaded before they are used, or they will error, unlike Java, which causes classes to load automatically as long as the class is used. The code for the load class is as follows:

function My_autoloader (Class) {
Include ' classes/'. class. '. class.php ';
}
Spl_autoload_register (' My_autoloader ');

PHP does not have multiple threads

PHP constants are defined not using final but with the defined () function. Example: Define ("CONSTANT", "Hello World");

PHP 5.3.0 also supports this way of defining: Const CONSTANT = ' Hello world ';

PHP calls the parent class's constructor instead of using super (), but Parent::__construct ().

PHP has destructors, similar to the Java Finalize method. For example: function __destruct () {print "Destroying";}

PHP uses this to represent the object, which is the same as the Java

PHP uses the parent to represent the parental class, which is the same as the Java super

PHP uses self to represent the current class, Java does not have this keyword, and it does not need to use a presentation class.

When calling static variables and methods in this class, PHP must use the Self keyword and::, Sample code:

Class A () {
public static function foo () {echo "foo";}
Self:: foo ();
}
If you are not in this class, use the class name directly:
Class A () {
public static function foo () {echo "foo";}
}
A:: foo ();

Not to be continued ...

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.