【问题标题】:parse error, expecting 'T_FUNCTION' & class error function in php [closed]解析错误,期待 'T_FUNCTION' & php 中的类错误函数 [关闭]
【发布时间】:2014-02-07 13:23:33
【问题描述】:

我收到这样的错误

解析错误,预计第 4 行出现“T_FUNCTION”

但是当我不使用类时,这些代码可以正常工作。在我没有在 php 代码中使用类之前。我只是写没有类的php代码。现在,我想在我的 php 代码中使用类,所以我实现了一些代码。但这给我带来了上述错误。我在哪里犯了错误? (我以为我在类函数中犯了错误)。

config.php

<?php
class Configuration
{
    private $user = "root";
    private $password = "password";
    public $conn;

    public static function connect()
    {
        if(self::$instance = null)
        {
            try
            {
                $conn = new PDO('mysql:host=localhost;dbname=database_table', $user, $password);
                $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            }
            catch(PDOException $e)
            {
                'Database Connection Error' .$e->getMessage();
            }
        }
        return
    }
}
?>

post.php

<?php
class StoreDatas
{
    include_once('config.php');
    if($_POST["register"] == "alldetails")
    {
        private $r_uname = trim($_POST["uname"]);
        private $r_email = trim($_POST["email"]);
        //some codes
            //some codes
            //some codes

        // name validation
        if(!preg_match("/^[a-zA-Z ]+$/", $r_uname))
        {
            echo '<b>Name</b> - Name must be from letters and spaces only';
            die();
        }
        // email validation
        if(!preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $r_email) )
        {
            echo '<b>Email</b> - Email must comply with this mask: chars(.chars)@chars(.chars).chars(2-4)';
            die();
        }
        //some codes
            //some codes
            //some codes

        function InsertDatas
        {
            try
            {
                $stmt = Connection::connect()->prepare("INSERT INTO user_registration ( Name, EmailID, MobileNumber, Password, ReEnterPassword, UserProfileCreatedOn ) VALUES ( ?, ?, ?, ?, ?, NOW() )");
                $conn->errorInfo();
                $stmt->bindParam('1', $r_uname, PDO::PARAM_STR);
                $stmt->bindParam('2', $r_email, PDO::PARAM_STR);
                $stmt->bindParam('3', $r_mobile, PDO::PARAM_STR);
                $stmt->bindParam('4', $r_password, PDO::PARAM_STR);
                $stmt->bindParam('5', $r_reenterpassword, PDO::PARAM_STR);
                $stmt->execute();
                echo 'Registered Succesfully. You can login now';
                echo '<script type="text/JavaScript">$("#registrationform")[0].reset();</script>';
            }
            catch(PDOException $e)
            {
                echo 'Failed to insert. Contact your admin' . $e->getMessage();
            }
        }
    }
}   
?>

【问题讨论】:

    标签: php mysql class oop pdo


    【解决方案1】:

    此语法无效

    <?php
    class StoreDatas
    {
        include_once('config.php');
        if($_POST["register"] == "alldetails")
        {
    

    您不能在类定义中使用任意代码。一切都应该在属性或函数内部。

    所以这段代码应该在某个方法中,可能是构造函数

    这样做也没有意义。您的方法和属性不能在条件内,也不一定非要如此。

    class X {
        if(true) {
            public function Y();
        }
    }
    

    是无效的语法,你也不需要它。如果不显式调用,该方法将不会被执行。

    还有:

    从请求中初始化数据的属性应该在构造函数(或用户定义的setter)中完成,而不是在类定义中。

    无效:

    class X {
        private $_y = $_POST['y'];
    }
    

    应该是

    class X {
        private $_y;
        public function __construct() {
            $this->_y = $_POST['y'];
       }
    }
    

    【讨论】:

      【解决方案2】:

      只有属性或方法可以声明为类成员。

      要包含文件,您可以将所有 includeif 添加到 __construct() 方法中,或者直接从类中添加。要保留大部分代码,您可以执行以下操作:

      include_once('config.php');
      
      class StoreDatas
      {
        public $r_uname;
        public $r_email;
        public function InsertDatas()
        {
           // ...
        }
      }
      
       // The code out of the class
      
       $store_data = new StoreDatas();
       if($_POST["register"] == "alldetails")
       {
         $store_data->r_uname = trim($_POST["uname"]);
         $store_data->r_email = trim($_POST["email"]);
         //some codes
      
         // name validation
         if(!preg_match("/^[a-zA-Z ]+$/", $r_uname))
         {
           echo '<b>Name</b> - Name must be from letters and spaces only';
           die();
         }
         // email validation
         if(!preg_match("/^[a-zA-Z]\w+(\.\w+)*\@\w+(\.[0-9a-zA-Z]+)*\.[a-zA-Z]{2,4}$/", $r_email) )
          {
            echo '<b>Email</b> - Email must comply with this mask: chars(.chars)@chars(.chars).chars(2-4)';
            die();
          }
          //some code
          //some code
          //some code
      }
      

      【讨论】:

      • -1:你让 OP 的代码更糟
      猜你喜欢
      • 2011-07-23
      • 2011-04-13
      • 2015-03-04
      • 2015-05-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-20
      • 1970-01-01
      相关资源
      最近更新 更多