PHP basic syntax and the difference from Java

Source: Internet
Author: User

. Represents the string addition, and in Java. $ as a prefix for variables, in addition, the variable name definition rules with Java parameter passing and method return simultaneous reference need to prefix &

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 =< Span style= "Font-family:mathjax_main; Font-style:normal; Font-weight:normal; " > " 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 ; v a Span class= "Mi" id= "mathjax-span-353" style= "Font-family:mathjax_math; Font-style:italic; " >r a="usea";// ais aaamakewith The value of a is used as the variable name variable, or you can use 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 a multi-threaded PHP constant defined by using the defined () function instead of final. 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 indicate that when an object, which is the same as Java's thisphp, uses parent to represent the parent class, which is used with Java superphp to represent the current class, Java does not have this keyword, and 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.

PHP basic syntax and the difference from Java

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.