PHP学习笔记之二 php入门知识点小结,学习php的朋友可以参考下。
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),'
';
array_push($capital,"Paris");
$newarray=array_merge($city,$capital);
foreach($newarray as $elem)
echo $elem.'
';
?>
复制代码 代码如下:
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";
?>
复制代码 代码如下:
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();
?>
复制代码 代码如下:
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);
?>