解析错误,期待php中的'T_FUNCTION'和类错误函数

Kar*_* RK -2 php mysql oop pdo class

我收到这样的错误

解析错误,期望第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
    }
}
?>
Run Code Online (Sandbox Code Playgroud)

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();
            }
        }
    }
}   
?>
Run Code Online (Sandbox Code Playgroud)

Roy*_* Bg 5

此语法无效

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

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

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

它也没有意义这样做.你的方法和属性不能在内部条件下,而且不一定是这样.

class X {
    if(true) {
        public function Y();
    }
}
Run Code Online (Sandbox Code Playgroud)

语法无效,您也不需要它.如果您没有显式调用它,则不会执行该方法.

也:

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

无效:

class X {
    private $_y = $_POST['y'];
}
Run Code Online (Sandbox Code Playgroud)

它应该是

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