PHP basic syntax and the difference from Java

Source: Internet
Author: User

. Represents a string addition, and a. $ in Java as a prefix for variables. Variable name definition rule with Java parameter passing and method return simultaneous references need to be prefixed &

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

Reflection calling method in Java variable function in PHP corresponding to: 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 can define mutable variables, that is, when defining variables. The variable name itself can be used with a variable.

Demo Sample 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 the variable name of the variable. can also use Var{a}= "Use a";

PHP classes must be loaded before they are used, otherwise they will error, unlike Java's use only to the class will cause the class to load itself actively. The code that loads the class such as the following:

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.

Demo Sample: 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 parent class when the object, which is the same as Java's thisphp, uses parent. This is the same as Java superphp, which uses self to represent the current class. Java does not have this keyword. There is no need to use a presentation class.

When PHP calls static variables and methods in this class, it must use Selfkeyword and:: To demonstrate the sample code:

Class A () {
public static function foo () {echo "foo";}
Self:: foo ();
}
Assume that the class name is not used directly in this class:
Class A () {
public static function foo () {echo "foo";}
}
A:: foo ();

Not to be continued.

。

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.