在php类中设置公共变量


Setting common variable inside a php class

下面的php类是我写的,它工作得很好。但是,我希望代码表示可以更专业。

它在做什么?

  • 在代码末尾,FULL_SEO_URL('xxxx')触发函数FULL_SEO_URL操作类;
  • 在类中,第二个方法将检查自身是否循环,并对某些处理采用第一个方法;
  • 第二个方法完成后,产品将传递给第三个方法

我想问什么?

  • 我怎么能全局变量内的类,但外面的方法?(例如$all_seo_url_array),实际上该变量在所有方法中都可用;
  • 我如何在类中设置$k=0的初始值?实际上,我还想重置它为0为每个FULL_SEO_URL('xxxx')的使用;

谢谢。

<?php
$k = 0; //I don't like to set $k=0 here, but it is the only way to work
class CONVERT{  
    public static function parent_id_to_seo_url($page_id){
        $sql_parent_id_to_seo_url   = "select * from ".DB_PREFIX."page 
                                    left join ".DB_PREFIX."page_contents 
                                    on ".DB_PREFIX."page.page_id = ".DB_PREFIX."page_contents.page_id
                                    where ".DB_PREFIX."page.page_id = '".$page_id."'
                                    and ".DB_PREFIX."page_contents.language = '".$_SESSION['frontend']['lang']."'";
        $result_parent_id_to_seo_url = mysql_query($sql_parent_id_to_seo_url);
        $record_parent_id_to_seo_url = mysql_fetch_assoc($result_parent_id_to_seo_url);
        return $record_parent_id_to_seo_url['seo_url'];
    }
    public static function all_seo_url($single_seo_url){
        global $all_seo_url_array;    //can I global it outside the methods?
        global $k;    //can I global it outside the methods?
        $sql_all_seo_url        =   "select * from ".DB_PREFIX."page 
                                    left join ".DB_PREFIX."page_contents 
                                    on ".DB_PREFIX."page.page_id = ".DB_PREFIX."page_contents.page_id
                                    where ".DB_PREFIX."page_contents.seo_url = '".$single_seo_url."'
                                    and ".DB_PREFIX."page_contents.language = '".$_SESSION['frontend']['lang']."'";
        $result_all_seo_url     = mysql_query($sql_all_seo_url);
        $record_all_seo_url = mysql_fetch_assoc($result_all_seo_url);
        $all_seo_url_array[$k]['seo_url'] = $record_all_seo_url['seo_url'];
        $k++;
        if ($record_all_seo_url['level']>1){CONVERT::all_seo_url(CONVERT::parent_id_to_seo_url($record_all_seo_url['parent_id']));} else { $k = 0; }  //can I reset $k=0 at better place?
        return $all_seo_url_array;
    }
    public static function reverse_seo_url($all_seo_url_array){
        global $all_seo_url_array_reverse;   //can I global it outside the methods?
        $all_seo_url_array_reverse = array_reverse($all_seo_url_array);
        $final_url = '';
        foreach ($all_seo_url_array_reverse as $key => $value){
            $final_url  .= '/'.$all_seo_url_array_reverse[$key]['seo_url'];
        }   
        return $final_url;
    }
}
function FULL_SEO_URL($seo_url){
    return CONVERT::reverse_seo_url(CONVERT::all_seo_url($seo_url));
}
echo FULL_SEO_URL('history'); echo "<br/>";
echo FULL_SEO_URL('other-innovations'); echo "<br/>";
?>

您可以在类中设置静态属性,如:

class myClass{
    public static $myProperty = 0;
    public static function myFunction(){
        echo self::$myProperty;
        self::$myProperty = 1;
        echo self::$myProperty;
    }
echo myClass::$myProperty;
# output: 0
myClass::myFunction();
# output 0 1
echo myClass::$myProperty;
# output 1
myClass::$myProperty=0;
echo myClass::$myProperty;
# output 0

我不确定我理解你所说的关于$k的全局是什么意思;

<?php
class CONVERT
{
    protected $k;
    function __construct()
    {
        $this->k = 0;
    }
}

这样,您可以在所有方法中使用$this->k

可以使用构造函数跨类访问变量。

class testclass
{
    private $global_variable;
    public function __construct($global_variable)
    {
       $this->global_variable = $global_variable;
    }
    public function yourmethod()
    {
       $this->global_variable; // And so on
    }
}