PHP study notes 2

  • 2020-03-31 21:33:21
  • OfStack

An array of 1.
PHP's array is actually an associative array, or hash table. PHP does not require you to declare the size of an array in advance, and you can create an array by direct assignment. Such as:
// the most traditional way is to use Numbers as keys and assign values
$state [0] = "Beijing";
$state [1] = "Hebei";
$state [2] = "Tianjin";
// if the key is an increasing number, you can omit it
$city [] = "Shanghai";
$city [] = "Tianjin";
$city [] = "through";
// use string as key
$capital [" China "] = "Beijing";
$capital (" Japan ") = "Tokyo";
Array () is a more convenient way to create an array. You can pass an array element as an argument to an array, or you can use => Operator to create an associative array. Such as:
Hc-positie $p = array (1);
$capital = array (" China "= >" Beijing ", "Japan = >" Tokyo ");
Array is a syntax, not a function. Similar to array, there is a list() that can be used to extract values from an array and assign values to multiple variables. Such as:
The list ($s, $t) = $city;
Echo $s, ' ', $t;
Output :Shanghai Tianjin
Note that the list method can only be used for arrays indexed by Numbers.
PHP built-in some commonly used array processing functions, you can refer to the manual. Common functions include count or sizeof to get the length of an array, array_merge to merge two or more arrays, and array_push (pop) to use an array like a stack.
 
<?php 
$state[0]="Beijing"; 
$state[1]="Hebei"; 
$state[2]="Tianjin"; 
$city[]="Shanghai"; 
$city[]="Tianjin"; 
$city[]="Guangzhou"; 
$capital["China"]="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 result is:
3
Shanghai
Tianjin
through
Beijing
Tokyo
Paris
2. Classes and objects
PHP5 is starting to have good support for object-oriented programming. The concept of a class in PHP is very similar to other object-oriented languages such as C#, which is also an aggregate of values and methods defined using the class keyword. Such as:
 
<?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 somewhat useless but syntactically complete class. The name of the class is first defined using the class keyword, and fields and methods can be defined internally. Modifiers for fields and methods can be private,protected,public, and final (only methods have them). Its meaning is consistent with other languages. I won't go into that. The difference is that PHP does not support overloading functions. Also, the constructor of PHP5 is defined as arbitration, note that the prefix is two underscores. The constructor for PHP4 is defined in the same way as the class name in other languages, and PHP5 is compatible with this approach. PHP5 also supports a destructor called s/s destruct. Inside the function, you can use the $this variable to get a reference to the current object. PHP also supports static functions, again decorated with the static keyword. The last function in the example is a static function. Static functions cannot be referenced by an instance of a class.
The following is an example of code that USES a class. PHP also instantiates a class with the new keyword. Through - > Operator to refer to methods of an object. Note that its static class references are ::, which is consistent with C++.
The following is a brief introduction to class inheritance. PHP USES the extends keyword for class inheritance, which is consistent with Java:
 
<?php 
class BaseClass { 
function __construct() { 
print "In BaseClass constructorn"; 
} 
} 
class SubClass extends BaseClass { 
function __construct() { 
parent::__construct(); 
print "In SubClass constructorn"; 
} 
} 
$obj = new BaseClass(); 
$obj = new SubClass(); 
?> 

The output is: In BaseClass constructor In BaseClass constructor In SubClass constructor
Note that the constructor of a subclass of PHP does not automatically call the constructor of the superclass and must be explicitly called in the program. Use the parent keyword to get a reference to the parent class. Also, because PHP itself is weakly typed, the concept of "polymorphism" is dead; in fact, it will always be polymorphic.
interface
An interface defines a set of methods, but does not implement them. Its syntax is:
Interface IInterfaceName
{
// constant, function definition
The} class implements an interface on the surface using the implements keyword, which is consistent with Java.
 
<?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); 
?> 

The instanceof keyword is added by PHP5 to determine whether an object is an instanceof a class or whether its type is an interface.

Related articles: