doudi7782 2011-07-20 22:10
浏览 7

课堂下的课程

Hello i just stuck in syntax, can anyone help me with next code?

class User {    
  protected $userID = preg_replace('#[^0-9]#i', '', $_SESSION['user_id']);
  protected $useremail = preg_replace('#[^A-Za-z0-9@_.-]#i', '', $_SESSION['user']);
  protected $userPassword = preg_replace('#[^A-Za-z0-9]#i', '', $_SESSION['user_password']);

  public function CheckUserLogin(){     
    if(!isset($_SESSION['user'])){
      header("location: login.php");
      exit();    
    }

    //Try to find user from session data in database
    $sql = "SELECT * FROM users WHERE id = '$this->userID' AND email = '$this->useremail' AND password = '$this->userPassword' LIMIT 1";
    $res = mysql_query($sql) or die(mysql_error());
    $userMatch = mysql_numrows($res);
    if ($userMatch == 0) {
      header("location: login.php");
      exit();
    }
  }
}
  • 写回答

2条回答 默认 最新

  • du7979 2011-07-20 22:18
    关注

    First of all, note that when declaring a property, you cannot assign it a value that's not know at compile-time -- which means you cannot call a function to initialize a property.

    This code :

    protected $userID = preg_replace('#[^0-9]#i', '', $_SESSION['user_id']);
    

    Is not valid.


    As a reference, you can read the Properties page of the manual (quoting the relevant sentence) :

    They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration.

    This declaration may include an initialization, but this initialization must be a constant value -- that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.


    You should first declare the property ; and, later (in the constructor of your class, for instance), initialize it :

    class User {
    
        protected $userID;
    
        public function __construc() {
            $this->userID = preg_replace('#[^0-9]#i', '', $_SESSION['user_id']);
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值