PHP Study Notes II php getting started

Source: Internet
Author: User
Tags php introduction
PHP Study Notes II php introduction knowledge point summary, learning php friends can refer.

PHP Study Notes II php introduction knowledge point summary, learning php friends can refer.

PHP study Note 2
1. Array
The PHP array is actually an associated array, or a hash table. PHP does not need to declare the array size in advance. You can create an array by directly assigning values. For example:
// The most traditional method: use numbers as keys and assign values.
$ State [0] = "Beijing ";
$ State [1] = "Hebei ";
$ State [2] = "Tianjin ";
// If the key is an incremental number, it can be omitted
$ City [] = "Shanghai ";
$ City [] = "Tianjin ";
$ City [] = "Guangzhou ";
// Use a string as the key
$ Capital ["China"] = "Beijing ";
$ Capital ["Japan"] = "Tokyo ";
It is more convenient to use array () to create an array. You can use an array element as an array parameter and use the => operator to create an associated array. For example:
$ P = array (1, 3, 5, 7 );
$ Capital = array ("China" => "Beijing", "Japan =>" Tokyo ");
Array is actually a syntax structure, not a function. Similar to array, there is also a list (), which can be used to extract values from the array and assign values to multiple variables. For example:
List ($ s, $ t) = $ city;
Echo $ s, '', $ t;
Output result: Shanghai Tianjin
Note: The list method can only be used for arrays indexed by numbers.
PHP has built-in some common array processing functions. For details, refer to the manual. Examples of common functions are as follows. count or sizeof can get the length of the array. array_merge can combine two or more arrays. array_push (pop) can use arrays like stacks.

The Code is as follows:


$ State [0] = "Beijing ";
$ State [1] = "Hebei ";
$ State [2] = "Tianjin ";
$ City [] = "Shanghai ";
$ City [] = "Tianjin ";
$ City [] = "Guangzhou ";
$ Capital ["China"] = "Beijing ";
$ Capital ["Japan"] = "Tokyo ";
Echo count ($ city ),'
';
Array_push ($ capital, "Paris ");
$ Newarray = array_merge ($ city, $ capital );
Foreach ($ newarray as $ elem)
Echo $ elem .'
';
?>


Output result:
3
Shanghai
Tianjin
Guangzhou
Beijing
Tokyo
Paris
2. Classes and objects
PHP5 started to support object-oriented programming. The class concept in PHP is very similar to other object-oriented languages such as C #. It is also a combination of values and methods and is defined using the class keyword. For example:

The Code is as follows:


Class AuthUser {
Protected $ userName;
Protected $ password;
Public function _ construct ($ userName, $ password ){
$ This-> userName = $ userName;
$ This-> password = $ password;
}
Public function GetUserName (){
Return $ userName;
}
Public function ChangePassword ($ old, $ new ){
If ($ this-> password = $ old ){
$ This-> password = $ new;
Return true;
} Else
Return false;
}
Public function Login ($ password ){
Return $ this-> password = $ password;
}
Public static function CreateUser ($ userName, $ password ){
$ User = new AuthUser ($ userName, $ password );
Return $ user;
}
}
$ User = AuthUser: CreateUser ("Admin", "123 ");
Echo $ user-> GetUserName ();
If ($ user-> ChangePassword ('abc', 'new '))
Echo 'changepassword success ';
Else
Echo 'change Password fail ';
$ User-> ChangePassword ("123", "321 ");
If ($ user-> Login ("321 "))
Echo "Login ";
Else
Echo "Login fail ";
?>


The above is a class with a complete syntax structure although it is useless. First, use the class keyword to define the class name. Fields and methods can be defined internally. The modifiers for fields and methods can be private, protected, public, and final (only methods are available ). Its meaning is consistent with that in other languages. I will not go into details. The difference is that PHP does not support function overloading. In addition, the PHP5 constructor is defined as _ construct. Note that the prefix is two underscores. The definition of PHP4 constructor is the same as that of other languages. PHP5 is also compatible with this syntax. PHP5 also supports destructor named _ destruct. Within the function, you can use the $ this variable to obtain the reference of the current object. PHP also supports static functions, which are also modified using the static keyword. In this example, the last function is a static function. Static functions cannot be referenced by class instances.
The following is a sample code for using a class. PHP also instantiates a class using the new keyword. Use the-> operator to reference object methods. Note that the reference method of its static class is:, which is consistent with that of C ++.
Next, we will briefly introduce the inheritance of classes. In PHP, The extends keyword is used to implement class inheritance, which is consistent with Java:

The Code is as follows:


Class BaseClass {
Function _ construct (){
Print "In BaseClass constructor \ n ";
}
}
Class SubClass extends BaseClass {
Function _ construct (){
Parent: :__ construct ();
Print "In SubClass constructor \ n ";
}
}
$ Obj = new BaseClass ();
$ Obj = new SubClass ();
?>


The output result is: In BaseClass constructor In SubClass constructor.
Note that the constructor of the PHP subclass does not automatically call the constructor of the parent class and must be explicitly called in the program. You can use the parent keyword to obtain the reference of the parent class. In addition, because PHP itself is a weak type, the concept of "polymorphism" does not exist. In fact, it will always be polymorphism.
Interface
Interfaces define a group of methods, but do not implement them. Its syntax is:
Interface IInterfaceName
{
// Constant and Function Definition
} Class uses the implements keyword to implement an interface on the surface, which is consistent with Java.

The Code is as follows:


Interface IAddable {
Function Add ($ something );
}
Class AddClass implements IAddable
{
Private $ data;
Function AddClass ($ num ){
$ Data = $ num;
}
Public function Add ($ something)
{
$ Data + = $ something;
Return $ data;
}
}
$ A = new AddClass (5 );
Echo $ a instanceof IAddable;
Echo $ a-> Add (10 );
?>


The instanceof keyword is added to PHP5 to determine whether an object is an instance of a certain class or whether its type is to implement an interface.

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.