Home > Backend Development > PHP Tutorial > 静态变量/方法问题

静态变量/方法问题

WBOY
Release: 2016-06-06 20:20:31
Original
1224 people have browsed it

<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>
Copy after login
Copy after login

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>
Copy after login
Copy after login

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>
Copy after login

执行结果:

<code>ttt
array(0) {
}</code>
Copy after login
Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template