The second of PHP learning notes
1. Arrays
An array of PHP is actually an associative array, or a hash table. PHP does not need to pre-declare the size of the array, it can be directly assigned to create the array. For example:
The most traditional, with the number of keys, 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 ["China"]= "Beijing";
$capital ["Japan"]= "Tokyo";
It is more convenient to use array () to create an array, you can pass an array element as a parameter to a set, or you can create an associative array with the-= operator. For example:
$p =array (1,3,5,7);
$capital =array ("China" = "Beijing", "japan=>" Tokyo ");
Array is actually a syntactic structure, not a function. Like an 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 has built a number of commonly used array processing functions, in particular, refer to the manual. Examples of commonly used functions are as follows: Count or sizeof can get the length of the array, Array_merge can merge two, or multiple arrays, Array_push (POP) can use the array like a stack.
Copy CodeThe 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. '
';
?>
The output is:
3
Shanghai
Tianjin
Guangzhou
Beijing
Tokyo
Paris
2. Classes and objects
PHP5 began 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 #, which is also a aggregation of values and methods, defined using the class keyword. For example:
Copy CodeThe 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 more complete syntax structure, though nothing is used. First, you define the name of the class by using the Class keyword, and you can define fields and methods internally. The modifiers for fields and methods can be private,protected,public and final (methods only). Its meaning is consistent with other languages. Don't dwell on it. The difference is that PHP does not support overloading of functions. In addition, the PHP5 constructor is defined as __construct, noting that the prefix is two underscores. The definition of PHP4 's constructor is consistent with other languages, and is the same as the class name, and PHP5 is also compatible with this notation. 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 the class.
Class is defined below is a code example that uses a class, and PHP instantiates a class with the new keyword. The method by which the object is referenced by the operator. Note that the method of referencing the 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:
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 in BaseClass constructor in subclass constructor
Note that the constructor for a subclass of PHP does not automatically invoke the constructor of the parent class and must be called explicitly in the program. You can use the parent keyword to get a reference to the parents class. In addition, because PHP itself is a weak type, so the concept of "polymorphic" does not exist, in fact, it is always polymorphic.
Interface
The interface defines a set of methods, but does not implement them. Its syntax is:
Interface Iinterfacename
{
constants, function definitions
} class uses the Implements keyword to implement an interface on the surface, which is consistent with Java.
Copy CodeThe 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);
?>
Where the instanceof keyword is PHP5 added to determine whether an object is an instance of a class, or if its type is to implement an interface.
http://www.bkjia.com/PHPjc/322850.html www.bkjia.com true http://www.bkjia.com/PHPjc/322850.html techarticle PHP Learning Note 21. An array of PHP arrays is actually an associative array, or a hash table. PHP does not need to pre-declare the size of the array, you can use the direct assignment of the way to create the array ...