在php类

时间:2015-08-19 13:53:34

标签: php class

以下php类是由我编写的,它工作正常。但是,我希望代码演示可以更专业。

它在做什么?

  • 在代码末尾,FULL_SEO_URL('xxxx')触发函数 FULL_SEO_URL来操作一个类;
  • 在课堂内,第二种方法将自行检查循环,并采用第一种方法进行一些处理;
  • 完成第二种方法后,产品将转到第三种方法

我想问什么?

  • 我怎样才能在类中扩展变量,但在方法之外呢? (例如$ all_seo_url_array),实际上变量在所有方法中都可用;
  • 如何在班级内设置$ k = 0的初始值?实际上我也想为每个FULL_SEO_URL('xxxx')用法重置为0;

感谢。

<?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/>";
?>

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

我不确定我理解全球关于$k;

的意思
<?php

class CONVERT
{
    protected $k;

    function __construct()
    {
        $this->k = 0;
    }
}

有了这个,你可以在所有方法中使用$this->k

答案 2 :(得分:0)

您可以使用构造函数访问整个类中的变量。

EX。

class testclass
{
    private $global_variable;

    public function __construct($global_variable)
    {
       $this->global_variable = $global_variable;
    }

    public function yourmethod()
    {
       $this->global_variable; // And so on
    }
}