The second of the PHP learning notes

Source: Internet
Author: User
Tags define array arrays definition functions implement inheritance static class
1. Array
The PHP array is actually an associative array, or a hash table. PHP does not need to declare the size of an array in advance, and you can create an array in a way that is directly assignable. For example:
Most traditional, use numbers to do the key, assign value
$state [0]= "Beijing";
$state [1]= "Hebei";
$state [2]= "Tianjin";
If the key is an incremented number, you can omit the
$city []= "Shanghai";
$city []= "Tianjin";
$city []= "Guangzhou";
Make a key with a string
$capital ["Beijing"]= ";
$capital ["Japan"]= "Tokyo";
It is more convenient to use array () to create an array, you can pass an array element to him as an array parameter, or you can create an associative array with the => operator. For example:
$p =array (1,3,5,7);
$capital =array ("=>", "Beijing", "japan=>" Tokyo);
Array is actually a grammatical structure, not a function. Like array, there is also a list (), which can be used to extract values from an array and assign values to multiple variables. For example:
List ($s, $t) = $city;
Echo $s, ', $t;
Output result: Shanghai Tianjin
Note that the list method can only be used for arrays that are indexed by numbers.
PHP built a number of commonly used array processing functions, can refer to the manual. Examples of commonly used functions are as follows: Count or sizeof can get the length of an array, Array_merge can combine two or more arrays, and array_push (POPs) can use arrays like stacks.
Copy CodeThe code is as follows:
<?php
$state [0]= "Beijing";
$state [1]= "Hebei";
$state [2]= "Tianjin";
$city []= "Shanghai";
$city []= "Tianjin";
$city []= "Guangzhou";
$capital ["Beijing"]= ";
$capital ["Japan"]= "Tokyo";
echo Count ($city), ' <br/> ';
Array_push ($capital, "Paris");
$newarray =array_merge ($city, $capital);
foreach ($newarray as $elem)
echo $elem. ' <br/> ';
?>

The output results are:
3
Shanghai
Tianjin
Guangzhou
Beijing
Tokyo
Paris
2. Classes and objects
PHP5 is beginning to have a good support for object-oriented programming. The concept of classes in PHP is very similar to other object-oriented languages such as C #, and it is also an aggregation of values and methods, defined using the class keyword. For example:
Copy CodeThe code is as follows:
<?php
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";
?>

Above is a class with a more complete grammatical structure, although it is not used. First, you use the class keyword to define the name of the class, and you can define fields and methods internally. The modifiers for fields and methods can be private,protected,public and final (method only). Its meaning is consistent with other languages. Don't repeat it. The difference is that PHP does not support overloading of functions. In addition, the definition of the PHP5 constructor is __construct, note that the prefix is two underscores. PHP4 's constructor definition is consistent with other languages, and is the same as the class name, and PHP5 is also compatible with this method. PHP5 also supports destructors, whose name is __destruct. Inside a function, you can use the $this variable to get a reference to the current object. PHP also supports static functions, which are also decorated with the static keyword. The last function in the example is a static function. A static function cannot be referenced by an instance of a class.
Class is defined under the code example that uses the class, and PHP instantiates a class using the New keyword. A method that references an object through the-> operator. Note that the reference method for its static class is:: This is consistent with C + +.
The following is a brief introduction to the inheritance of the following classes. PHP uses the extends keyword to implement class inheritance, which is consistent with Java:
Copy CodeThe code is as follows:
<?php
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 result of the output is: in BaseClass constructor into BaseClass constructor in subclass constructor
Note that the constructor for a subclass of PHP does not automatically call the parent class's constructor, which must be explicitly called in the program. Use the parent keyword to get a reference to the parent class. In addition, because PHP itself is weak type, so the concept of "polymorphism" does not exist, in fact, it is always polymorphic.
Interface
interface defines a set of methods, but does not implement them. Its syntax is:
Interface Iinterfacename
{
constants, function definitions
The class uses the Implements keyword to implement an interface on the surface, which is consistent with Java.
Copy CodeThe code is as follows:
<?php
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);
?>

Where the instanceof keyword is PHP5 added to determine whether an object is an instance of a class, or its type is not implemented by 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.