Basic PHP syntax and differences with Java

Source: Internet
Author: User
The basic PHP syntax and the difference with Java. string addition-> same as in Java. $ is the prefix of the variable. In addition, the variable name definition rule must be prefixed with the reference parameter passing in Java and returning methods &

Sample code:
Function f (& $ a) {}// the method parameter value is passed in as a reference
Functioin & f () {}// the return value of the method is returned as a reference.

In Java, the reflection call method corresponds to the variable function in PHP: var a = "a"; a (); PHP is more powerful than Java in defining variables and can define variable variables, that is to say, when defining a variable, the variable name itself can use a variable.

Sample code:
Var a = "a"; var a = "usea"; // a is a variable that uses the value of a as the variable name, you can also use var {a} = "use ";

PHP classes must be loaded before they are used. Otherwise, an error is reported. Unlike Java, classes are automatically loaded only when they are used. The code for loading a class is as follows:

Function my_autoloader (class ){
Include 'classes/'. class.'. class. php ';
}
Spl_autoload_register ('My _ autoloader ');

PHP does not define multithreading PHP constants. instead of using final, PHP uses the defined () function. Example: define ("CONSTANT", "Hello world .");

// This method is also supported after PHP 5.3.0: const CONSTANT = 'Hello World ';

PHP calls the constructor of the parent class instead of using super (), but parent ::__ construct (). PHP has destructor, similar to Java's finalize method. For example, function _ destruct () {print "Destroying";} PHP uses this to indicate an object. this is the same as Java's this PHP uses parent to represent the parent class, this is the same as that in Java's super PHP, which uses self to represent the current class. Java does not have this keyword and does not need to represent the class. When PHP calls static variables and methods in this class, it must use the self keyword and:, sample code:

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

Not complete...

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.