首頁  >  文章  >  後端開發  >  nginx - php基礎問題。 。

nginx - php基礎問題。 。

WBOY
WBOY原創
2016-09-08 08:43:521152瀏覽

某個項目的文件

<code><?php
/**
 * 基础类
 */
abstract class Ctrl_Base extends Yaf_Controller_Abstract{
    /**
     * 开启 SESSION : 1
     * 必须登录 : 2
     * 必须管理员 : 4
     */
    protected $_auth = 0;

    /**
     * 当前登录用户
     * @var array
     */
    public $mCurUser = array();
    public $l;
    public $lang;

    /**
     * 构造函数
     */
    public function init(){
        $tSqlite = $this->sqlite();
        $tLCount = $tSqlite->getRow("SELECT COUNT(0) c FROM sqlite_master where type='table' and name='lang'");
        $tLang = array();
        if(!empty($tLCount['c'])){$tLang = $tSqlite->getRow($tSql = 'select * from lang');}
        
        $tLang = empty($tLang['lang'])?'cn':$tLang['lang'];
        include realpath(dirname(__FILE__).'../../../lang/'.$tLang.'/common.php');
        $lang = json_encode($lang);
        $lang = json_decode($lang);
        $this->assign('lang' , $lang);
        $this->assign('l' , $tLang);
        $this->l = $tLang;
        $this->lang = $lang;
        //(1 & $this->_auth) && $this->_session();
        //(1 < $this->_auth) && $this->_role();
        $tSqlite->close();
    }</code>

問題一:

<code>        $lang = json_encode($lang);
        $lang = json_decode($lang);</code>

這兩句在這裡什麼作用? ,為什麼要把$lang變成json格式後又解析成php變數?這不是多此一舉麼?
問題二:

<code>        $this->assign('lang' , $lang);
        $this->assign('l' , $tLang);
        $this->l = $tLang;
        $this->lang = $lang;</code>

這四句又怎麼理解?
我的理解是assign的第一個參數是變數名,後面是它的變數值,可是

<code>`$this->l = $tLang;`不就是将`$tLang`赋值给`$this`的`l`属性么?这四句不就重复了么?</code>

回覆內容:

某個項目的文件

<code><?php
/**
 * 基础类
 */
abstract class Ctrl_Base extends Yaf_Controller_Abstract{
    /**
     * 开启 SESSION : 1
     * 必须登录 : 2
     * 必须管理员 : 4
     */
    protected $_auth = 0;

    /**
     * 当前登录用户
     * @var array
     */
    public $mCurUser = array();
    public $l;
    public $lang;

    /**
     * 构造函数
     */
    public function init(){
        $tSqlite = $this->sqlite();
        $tLCount = $tSqlite->getRow("SELECT COUNT(0) c FROM sqlite_master where type='table' and name='lang'");
        $tLang = array();
        if(!empty($tLCount['c'])){$tLang = $tSqlite->getRow($tSql = 'select * from lang');}
        
        $tLang = empty($tLang['lang'])?'cn':$tLang['lang'];
        include realpath(dirname(__FILE__).'../../../lang/'.$tLang.'/common.php');
        $lang = json_encode($lang);
        $lang = json_decode($lang);
        $this->assign('lang' , $lang);
        $this->assign('l' , $tLang);
        $this->l = $tLang;
        $this->lang = $lang;
        //(1 & $this->_auth) && $this->_session();
        //(1 < $this->_auth) && $this->_role();
        $tSqlite->close();
    }</code>

問題一:

<code>        $lang = json_encode($lang);
        $lang = json_decode($lang);</code>

這兩句在這裡什麼作用? ,為什麼要把$lang變成json格式後又解析成php變數?這不是多此一舉麼?
問題二:

<code>        $this->assign('lang' , $lang);
        $this->assign('l' , $tLang);
        $this->l = $tLang;
        $this->lang = $lang;</code>

這四句又怎麼理解?
我的理解是assign的第一個參數是變數名,後面是它的變數值,可是

<code>`$this->l = $tLang;`不就是将`$tLang`赋值给`$this`的`l`属性么?这四句不就重复了么?</code>

先不說程式碼寫的怎麼樣..

第一個問題, 就是把陣列轉換成物件.

第二個問題, assign是註冊模板變數, 在view裡面可以直接使用$lang. 所以是不一樣的

問題一:這裡$lang的變數不知道哪裡來的。 。 。 。不過這種json_encode()json_decode()連寫是可以做資料型別轉換的。如果最初$lang是一個array,執行完這兩句就轉換為object。

問題二:我也不是很清楚。 。

問題一:將$lang變數轉換為object(物件).$lang多半是include引入的。在模板引擎中,數組跟物件的格式是不一樣的。可能是作者用的順手,所以就轉了。
問題二:是將$lang和$tLang傳遞到頁面上方便頁面呼叫。而用了$this->l和$this->lang是將其保存在當前類別中方便自身呼叫或在外調用。而當我們每次new出該類別時,該類別會傳遞兩個變數到頁面,同時並將變數值賦值給目前類別的屬性。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn