首頁 >後端開發 >php教程 >静态变量/方法问题

静态变量/方法问题

WBOY
WBOY原創
2016-06-06 20:20:311215瀏覽

<code>class Common_Sms
{

    protected static $ms_conf = array(); 
    public function __construct(){

        self::$ms_conf = array(
            'url'        => '1212',
            'username'    => 'XXX',
            'password'    => 'OOO',
            'veryCode'    => 'KKK'
        ); 
    }

    
    private static function curl_post($method = '', $param = array()) {

        do {
            if(! $method || empty($param)) {

                $ret = array(
                        'status' => 'failed',
                        'msg'     => 'Invalid param',
                        'results'=> '无效的参数'
                    );
                break;
            }

            $url = self::$ms_conf['url'] .'?method='.$method;
    } while(0);
    
        return $url;
    }
    
    /**
     * 发送验证码短信
     * @param  [type] $mobile 手机号
     * @param  array  $param  captcha: array( 'code'=> , 'minute' => )
     * @return array         回传信息
     */

    public static function send($mobile = '', $param = array()){

        do{
            if(!$mobile)
            {
                $result = array('status' => 'failed', 'results' => '缺少[mobile] 参数');
                break;
            }
            
            if(!isset($param['code']))
            {
                $result = array('status' => 'failed', 'results' => '错误的[code] 参数');
                break;
            }

            $param_arr = array(
                'mobile'    => $mobile,
                'content'    => "@1@={$param['code']}",
                'sendtime'    => '',
                'msgtype'    => 2,
                'tempid'    => 'JSM40485-0001',
                'code'        => 'utf-8',
                );

            $req_result = self::curl_post('sendMsg', $param_arr);
            if($req_result['status'] == 'success')
            {
                // $req_result = trim($req_result);
                
                // $back_result = explode('#', $req_result);

                // if($back_result[0] == 0)
                // {
                //     $result = array('status' => 'success', 'results' => array('send' => $back_result[2], 'commit' => $back_result[1]));
                // }
                // else
                // {
                    
                //     $msg = self::sendError($back_result);
                //     $result = array('status' => 'failed', 'results' => $msg);
                // }    
                // 
                
                return $req_result['results'];
                
            }
            else
            {
                $result = array('status' => 'failed', 'results' => $req_result['msg']);
                break;
            }
            

        } while(0);

        return $result;
    }
}</code>

A PHP Error was encountered
Severity: Notice
Message: Undefined index: url
Filename: common/sms.php
Line Number: 32


没有系统的看过书,求解 !!!

回复内容:

<code>class Common_Sms
{

    protected static $ms_conf = array(); 
    public function __construct(){

        self::$ms_conf = array(
            'url'        => '1212',
            'username'    => 'XXX',
            'password'    => 'OOO',
            'veryCode'    => 'KKK'
        ); 
    }

    
    private static function curl_post($method = '', $param = array()) {

        do {
            if(! $method || empty($param)) {

                $ret = array(
                        'status' => 'failed',
                        'msg'     => 'Invalid param',
                        'results'=> '无效的参数'
                    );
                break;
            }

            $url = self::$ms_conf['url'] .'?method='.$method;
    } while(0);
    
        return $url;
    }
    
    /**
     * 发送验证码短信
     * @param  [type] $mobile 手机号
     * @param  array  $param  captcha: array( 'code'=> , 'minute' => )
     * @return array         回传信息
     */

    public static function send($mobile = '', $param = array()){

        do{
            if(!$mobile)
            {
                $result = array('status' => 'failed', 'results' => '缺少[mobile] 参数');
                break;
            }
            
            if(!isset($param['code']))
            {
                $result = array('status' => 'failed', 'results' => '错误的[code] 参数');
                break;
            }

            $param_arr = array(
                'mobile'    => $mobile,
                'content'    => "@1@={$param['code']}",
                'sendtime'    => '',
                'msgtype'    => 2,
                'tempid'    => 'JSM40485-0001',
                'code'        => 'utf-8',
                );

            $req_result = self::curl_post('sendMsg', $param_arr);
            if($req_result['status'] == 'success')
            {
                // $req_result = trim($req_result);
                
                // $back_result = explode('#', $req_result);

                // if($back_result[0] == 0)
                // {
                //     $result = array('status' => 'success', 'results' => array('send' => $back_result[2], 'commit' => $back_result[1]));
                // }
                // else
                // {
                    
                //     $msg = self::sendError($back_result);
                //     $result = array('status' => 'failed', 'results' => $msg);
                // }    
                // 
                
                return $req_result['results'];
                
            }
            else
            {
                $result = array('status' => 'failed', 'results' => $req_result['msg']);
                break;
            }
            

        } while(0);

        return $result;
    }
}</code>

A PHP Error was encountered
Severity: Notice
Message: Undefined index: url
Filename: common/sms.php
Line Number: 32


没有系统的看过书,求解 !!!

请问你怎么调用的?
你的那个 静态方法 是 private 的, 你怎么调用的这个方法?


静态方法在调用的时候, 类的构造函数是不会被自动调用的.
所以你的 $ms_conf 是一个空数组, 所以结果你应该懂的吧?

<code class="php"><?php class test{
    protected static $xx = array();

    public function __construct(){
        echo "call __construct\n";
        self::$xx['xxxx'] = 123;
    }

    public static function ttt(){
        echo "ttt\n";

        var_dump(self::$xx);
    }
}

test::ttt();</code></code>

执行结果:

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