The grammar basis of PHP learning essays

Source: Internet
Author: User
Tags php script

1. Functions

The function does not need to be defined before the call unless the function is conditionally defined in the following two examples.

When a function is conditionally defined, its definition must be processed before the call.

Example #2 Conditional Functions

<?php$makefoo = true;/* cannot invoke the Foo () function here   because it does not yet exist, but can call the bar () function. */bar (); if ($makefoo) {  function foo ()  {    echo "I don ' t exist until program execution reaches me.\n";  }}
/* can now safely call function foo (),
Because the $makefoo value is true */

if ($makefoo) foo ();

function Bar ()
{
echo "I exist immediately upon program start.\n";
}

?>

Functions in the Example #3 function

<?phpfunction foo () {  function bar ()  {    echo "I don ' t exist until Foo () is called.\n";  }} /* Cannot now call the bar () function because it does not yet exist */foo ();/* can now call the bar () function, because   the execution of the Foo () function causes the bar () function to become a defined function */bar (); > 

All functions and classes in PHP have global scope, can be defined within a function and are called outside, and vice versa.

PHP does not support function overloading, and it is not possible to cancel the definition or redefine a declared function.

Note: the function name is case-insensitive, but it is a good practice to use the same form as it was defined when calling a function.

PHP's functions support a variable number of parameters and default parameters. See Func_num_args (),Func_get_arg (), and Func_get_args ().

Recursive functions can be called in PHP. However, to avoid recursive function/method calls over 100-200 layers, because the stack might crash and then terminate the current script.

2. Class

A class can inherit the methods and properties of another class in a declaration using the extends keyword. PHP does not support multiple inheritance, and a class can inherit only one base class.

2.1 Properties

A variable member of a class is called a "property", or "field", "characteristic", which is known collectively as "Properties" in this document. A property declaration is made up of the keyword public,protected , or private , followed by a common variable declaration. A variable in a property can be initialized, but the initialized value must be a constant, where a constant is a PHP script that can get its value at compile time without relying on the information at run time to evaluate it.

Class 2.2 Constants

You can define a value that is always the same in a class as a constant. You do not need to use symbols when defining and using constants $ .

The value of a constant must be a fixed value, not a variable, a class property, a result of a mathematical operation, or a function call.

You can also define constants in the interface (interface).

2.3 Constructors and destructors

If a constructor is defined in a subclass, the constructor of its parent class is not implicitly called. To execute the constructor of the parent class, you need to call parent::__construct ()in the constructor of the child class. If a subclass does not have a constructor defined, it inherits from the parent class like a normal class method (if it is not defined as private).

As with constructors, the destructor of the parent class is not called by the engine. To execute a destructor for a parent class, you must explicitly call parent::__destruct ()in the destructor body of the subclass. Also, as with constructors, subclasses inherit the parent class if they do not have a destructor defined by themselves.

2.4 Static statically keyword

Declaring a class property or method as static can be accessed directly without instantiating the class. Static properties cannot be accessed through an object that a class has instantiated (but static methods can).

To be compatible with PHP 4, if no access control is specified, the properties and methods default to public.

Because static methods do not need to be called through objects, pseudo-variables $this are not available in static methods.

A static property cannot be accessed by an object through the operator.

Invoking a non-static method in a static manner results E_STRICT in a level of error.

Just like all other PHP static variables, static properties can only be initialized to literals or constants, and expressions cannot be used. You can initialize a static property to an integer or an array, but you cannot initialize it to another variable or function return value, or point to an object.

<?phpclass foo{public    static $my _static = ' Foo ';    Public Function Staticvalue () {        return self:: $my _static;    }} Class Bar extends foo{public    function foostatic () {        return Parent:: $my _static;    }} Print Foo:: $my _static. "\ n"; $foo = new Foo ();p rint $foo->staticvalue (). "\ n";p rint $foo->my_static. "\ n";      Undefined "Property" My_static print $foo:: $my _static. "\ n"; $classname = ' Foo ';p rint $classname:: $my _static. "\ n"; As of PHP 5.3.0print Bar:: $my _static. "\ n"; $bar = new bar ();p rint $bar->foostatic (). "\ n";? >   </programlisting>  </example>  <example>   <title> static Method Example </title >    <programlisting role= "PHP" ><![ Cdata[<?phpclass Foo {public    static function Astaticmethod () {        //...    

2.5 Abstract class

PHP 5 supports abstract classes and abstract methods. A class that is defined as abstract cannot be instantiated. Any class, if at least one of its methods is declared abstract, then the class must be declared abstract. A method that is defined as abstract simply declares its invocation method (parameter) and cannot define its specific function implementation.

When inheriting an abstract class, the subclass must define all the abstract methods in the parent class, and the access control for these methods must be the same (or looser) as the parent class. For example, if an abstract method is declared as protected, the methods implemented in the subclass should be declared as protected or public, and cannot be defined as private. The method must be called in the same way, i.e. the type and the required number of parameters must be the same. For example, a subclass defines an optional parameter, and the declaration of the parent abstract method does not, and the declaration does not conflict. This also applies to constructors from PHP 5.4. The constructor declaration before PHP 5.4 can be different.

2.6 Object Interface

Using interfaces (interface), you can specify which methods a class must implement, but you do not need to define the specifics of these methods.

Interfaces are defined by the interface keyword, just like defining a standard class, but all of the methods defined in it are empty.

All methods defined in the interface must be public, which is the attribute of the interface.

To implement an interface, use the implements operator. All methods defined in the interface must be implemented in the class, or a fatal error will be reported. Class can implement multiple interfaces, separating the names of multiple interfaces with commas

Constants can also be defined in an interface. Interface constants and class constants are used exactly the same, but cannot be overridden by subclasses or sub-interfaces

Abstract classes differ from object interfaces:

1), the use of the interface is through the keyword implements. The use of abstract classes is through the keyword extends. Of course the interface can also be inherited by the keyword extends.

2), the interface cannot declare member variables (including class static variables), but you can declare class constants. Abstract classes can declare various types of member variables to implement the encapsulation of data. (The member variables in the Java interface are declared as public static final type)

3), interfaces do not have constructors, abstract classes can have constructors.

4), the method in the interface is the public type by default, and the methods in the abstract class can be decorated with private,protected,public.

5), a class can implement multiple interfaces at the same time, but a class may inherit only one abstract class.

2.7 Overloading

"Overloading" in PHP is different from most other object-oriented languages. Traditional " overloads" are used to provide multiple class methods with the same name, but each method has a different parameter type and number.

PHP provides a " reload" (overloading) that refers to the dynamic " create" class properties and methods. We do this by means of magic methods.

Overloaded methods are called when a class property or method is called that is undefined or not visible under the current environment. these undefined or invisible class properties or methods are addressed by using the inaccessible properties and the inaccessible methods (inaccessible methods) later in this section.

All overloaded methods must be declared public.

Note: The parameters of these magic methods cannot be passed by reference.

Property overloadingpublic void __set ( string $name , mixed $value ) public mixed __get ( string $name ) public bool __isset ( string $name ) public void __unset ( string $name )

__set () is called when a value is assigned to an unreachable property.

__get () is called when the value of an inaccessible property is read.

When Isset () or empty () is called on an inaccessible property , __isset () is called.

When Unset () is called on an inaccessible property , __unset () is called.

Method overloadingpublic mixed __call ( string $name , array $arguments ) Public static mixed __callstatic ( string $name , array $arguments ) /c11>

When an inaccessible method is called in an object, __call () is called.

When a non-accessible method is called in a static manner, __callstatic () is called.

2.8 Magic Methods

__construct (), __destruct (), __call (), __callstatic (), __get (), __set (), __isset (), __unset (), __sleep (), __wakeup (), __ ToString (), __invoke (), __set_state (), __clone (), and __debuginfo () are known in PHP as " Magic Methods" (Magic methods). You cannot use these method names when naming your own class methods, unless you want to use their magic features.

2.9 Final keyword

PHP 5 has a new final keyword. If a method in the parent class is declared final, the child class cannot overwrite the method. If a class is declared final, it cannot be inherited.

Note: attributes cannot be defined as final, only classes and methods can be defined as final.

2.10 Object Comparisons

When comparing two object variables with the comparison operator (= =), the principle of comparison is that if the properties and property values of the two objects are equal, and two objects are instances of the same class, the two object variables are equal.

If you use the strict equality operator (= = =), the two object variables must point to the same instance of a class (that is, the same object).

2.11 Type constraints

PHP 5 can use type constraints. The parameters of the function can specify that the object must be (the name of the class specified in the function prototype), interface, Array (PHP 5.1), or callable (PHP 5.4). However, if you use NULL as the default value for the parameter, you can still use NULL as the argument when calling the function .

If a class or interface specifies a type constraint, all of its subclasses or implementations also do so.

Type constraints cannot be used with scalar types such as int or string. Traits is not allowed.

3. Namespaces

Although any valid PHP code can be included in a namespace, only three types of code are affected by the namespace, which are: classes, functions, and constants .

The namespace is declared by the keyword namespace . If a file contains a namespace, it must declare the namespace before all other code.

The only valid code before declaring the namespace is the declare statement that defines how the source file is encoded. In addition, all non-PHP code, including whitespace characters, cannot appear before the declaration of the namespace.

PHP Other language features are different, the same namespace can be defined in multiple files, that is, the content of the same namespace is allowed to be split into different files.

The grammar basis of PHP learning essays

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.