本文目錄一覽:
簡單分析PHP中序列化用法介紹
簡單分析PHP中序列化用法介紹
序列化在我們學習php中都會有用到了對於序列化我們常用的函數有serialize和unserialize了,希望以下這篇文章能夠幫助到各位了解到PHP中序列化用法,具體如下:
0x00 序列化函數
serialize():返回帶有變量類型和值的字符串
unserialize():想要將已序列化的字符串變回 PHP 的值
測試代碼:
?php
class test{
var $a;
var $b;
function __construct($a,$b,$c){
$a = $a;
$this-b = $b;
}
}
class test1 extends test{
function __construct($a){
$this-a = $a;
}
}
$a = ‘hello’;
$b = 123;
$c = false;
$d = new test(‘helloa’,’hellob’,’helloc’);
$e = new test1(‘hello’);
var_dump(serialize($a));
var_dump(serialize($b));
var_dump(serialize($c));
var_dump(serialize($d));
var_dump(serialize($e));
?
運行結果:
string ‘s:5:”hello”;’ (length=12)
string ‘i:123;’ (length=6)
string ‘b:0;’ (length=4)
string ‘O:4:”test”:2:{s:1:”a”;N;s:1:”b”;s:6:”hellob”;}’ (length=46)
string ‘O:5:”test1″:2:{s:1:”a”;s:5:”hello”;s:1:”b”;N;}’ (length=46)
序列化字符串格式: 變量類型:變量長度:變量內容 。
如果序列化的是一個對象,序列化字符串格式為:
變量類型:類名長度:類名:屬性數量:{屬性類型:屬性名長度:屬性名;屬性值類型:屬性值長度:屬性值內容}
將上述結果反序列化輸出,執行結果:
string ‘hello’ (length=5)
int 123
boolean false
object(test)[1]
public ‘a’ = null
public ‘b’ = string ‘hellob’ (length=6)
object(test1)[1]
public ‘a’ = string ‘hello’ (length=5)
public ‘b’ = null
0x01 對象序列化
當序列化對象時,PHP 將在序列動作之前調用該對象的成員函數 sleep()。這樣就允許對象在被序列化之前做任何清除操作。類似的,當使用 unserialize() 恢復對象時, 將調用 wakeup()成員函數。
在serialize()函數執行時,會先檢查類中是否定義了 sleep()函數,如果存在,則首先調用 sleep()函數,如果不存在,就保留序列字符串中的所有屬性。
在unserialize()函數執行時,會先檢查是否定義了 wakeup()函數。如果 wakeup()存在,將執行__wakeup()函數,會使變量被重新賦值。
serialize()測試代碼:
?php
class test{
var $a;
var $b;
function __construct($a,$b,$c){
$this-a = $a;
$this-b = $b;
}
function __sleep(){
echo “b has changed”.”\n”;
$this-b = ‘hib’;
return $this-b;
}
function __wakeup(){
echo “a has changed”.”\n”;
$this-a = ‘hia’;
}
}
class test1 extends test{
function __construct($a){
$this-a = $a;
}
}
$d = new test(‘helloa’,’hellob’,’helloc’);
$e = new test1(‘hello’);
serialize($d);
serialize($e);
var_dump($d);
var_dump($e);
?
執行結果:
b has changed b has changed
object(test)[1]
public ‘a’ = string ‘helloa’ (length=6)
public ‘b’ = string ‘hib’ (length=3)
object(test1)[2]
public ‘a’ = string ‘hello’ (length=5)
public ‘b’ = string ‘hib’ (length=3)
unserialize()測試代碼:
class test{
var $a;
var $b;
function __construct($a,$b,$c){
$this-a = $a;
$this-b = $b;
}
function __sleep(){
echo “b has changed”.”\n”;
$this-b = ‘hib’;
return $this-b;
}
function __wakeup(){
echo “a has changed”.”\n”;
$this-a = ‘hia’;
}
}
class test1 extends test{
function __construct($a){
$this-a = $a;
}
}
$d = ‘O:4:”test”:2:{s:1:”a”;N;s:1:”b”;s:6:”hellob”;}’ ;
如何分析PHP職位要點
php學習成本低,但是要精通也不簡單,寫出一個好程序需要對php的一些基本要點精通熟練否則會出現難以理解的錯誤。比如php的一些函數isset,is_null,empty的區別,其中又和php的數據類型關聯起來。如果判斷錯誤,會導致程序下一步操作出現錯誤(常見因為sql查詢條件為空出錯)。最好我們在程序中加入錯誤異常處理。
1,php語法,數據類型和php函數精通熟練。
2,懂得OOP編程概念並掌握一個php mvc框架,加快開發效率,增加代碼利用率和資源可重複利用,代碼也會更加清 晰明了,可維護性高。
3,安全性。了解網站常見的攻擊方式(XSS,csrf,sql-injection等)。
4,性能優化。(服務器,php,mysql,nosql)。
5,熟悉js,ajax和html。熟悉http/ip協議。
7,掌握lnmp,lamp,iis服務環境的搭建和php的運行原理。
php分析域名
php分析域名!寫法方法!調用就可以得出域名
? //轉載保留來源於 // ####################### 分析域名 ####################### function phpzygetname($url) { $referer = preg_replace(“/?://([^/]+) */i” ” ” $url); $referer = str_replace(” ” “” $referer); return $referer; } ? lishixinzhi/Article/program/PHP/201311/21148
原創文章,作者:XQVMC,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/324602.html