douyi02577 2018-03-09 14:43
浏览 48
已采纳

静态方法PHP-OOP

I want to access Static method properties to other static method. I am trying to access it via:

public static function getGender($user) {   
    $user_data[] = Common::getSomeUsers(true);
    $ssn_to_gender[] = array();
    foreach($user_data as $u_data){
        foreach($u_data as $key => $user){
            '$key:' .$user['ssn'];
            $ssn = $user['ssn'] . "<br/>";
            //echo $ssn;
            $ssn_gender= substr($ssn,7,-6) . "<br/>" ;
            $dob_gender= substr($ssn,0,6) . "<br/>" ;
            echo $dob_gender;
            if($ssn_gender % 2 == 0 )
            {
                echo 'F' . '</br>';
                $ssn_to_gender[$user['ssn']] = 'F';
            }
            elseif($ssn_gender % 2 == 1)
            {
                echo 'M' .'<br/>';
                $ssn_to_gender[$user['ssn']] = 'M';
            }

        }

    }
    return $ssn_to_gender;
    return $dob_gender;
    //$user= array();
    //echo Common::getSomeUsers($user);

}

public static function getAge( $user ) {


    self::getGender(true);
    Common::$dob_gender;

}
Common::getAge($userid='');
echo Common::$dob_gender;

I am trying to access $dob_gender variable in get_Age() method from getGender() method.

But unable to get the $dob_gender. Please guide I am new with PHP and object oriented programming.

Thanks.

  • 写回答

1条回答 默认 最新

  • douman9420 2018-03-11 01:46
    关注

    I would think about doing a standard method instead of a series of static methods:

    class Common
    {
        private   $ssn_to_gender,
                  $dob_gender;
        /**
        *  @returns  Itself (Common object)
        */
        public function getGender($user)
        {
            $user_data[]           =  $this->getSomeUsers(true);
            $this->ssn_to_gender[] =  array();
    
            foreach($user_data as $u_data){
                foreach($u_data as $key => $user){
                    $ssn              =  $user['ssn'];
                    $ssn_gender       =  substr($ssn,7,-6);
                    $this->dob_gender = substr($ssn,0,6);
    
                    if($ssn_gender % 2 == 0 ) {
                        $this->ssn_to_gender[$user['ssn']] = 'F';
                    }
                    elseif($ssn_gender % 2 == 1) {
                        $this->ssn_to_gender[$user['ssn']] = 'M';
                    }
                }
            }
    
            return $this;
        }
        /**
        *  @returns  The corresponding private variable
        */
        public function getSSNToGender()
        {
            return $this->ssn_to_gender;
        }
        /**
        *  @returns  The corresponding private variable
        */
        public function getDOBGender()
        {
            return $this->dob_gender;
        }
        /**
        *  @question   I am not sure the purpose of this method. Presumably you need to
        *              do more in the way of script otherwise you would just do the
        *              getGender() method instead
        */
        public function getAge($userid)
        {
            # I am returning this but presumably you want to do something else??
            return $this->getGender($userid);
        }
    }
    

    To use would be like:

    $Common = new Common();
    $Age    = $Common->getAge($userid);
    
    print_r($Age->getDOBGender());
    print_r($Age->getSSNToGender());
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝