欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

静态变量/方法问题

程序员文章站 2022-06-05 13:23:56
...
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;
    }
}

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


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

回复内容:

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;
    }
}

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


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

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


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

执行结果:

ttt
array(0) {
}
相关标签: php