php - fatal error : Destructor cannot take arguments

标签 php android mysql

我是编程新手... 我尝试从 android 调用网络服务。但我那里有 fatal error 。

1.这是android编码

public class UserFunction {
    private JSONParser jsonParser;

    private static String LoginURL="http://10.0.2.2/android/include/index.php";
    private static String RegisterURL="http://10.0.2.2/android/include/index.php";

    private static String login_tag="login";
    private static String register_tag="register";


    public UserFunction(){
        jsonParser=new JSONParser();
    }


    public JSONObject loginUser(String email, String password){
        //building parameter
        List<NameValuePair> params= new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", login_tag));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("password", password));
        JSONObject json=jsonParser.getJSONFromUrl(LoginURL, params);

        return json;
    }


    public JSONObject registerUser(String email, String name, String password){
        //building parameter
        List<NameValuePair> params= new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("tag", login_tag));
        params.add(new BasicNameValuePair("name", name));
        params.add(new BasicNameValuePair("email", email));
        params.add(new BasicNameValuePair("password", password));
        //getting JSONObject
        JSONObject json=jsonParser.getJSONFromUrl(RegisterURL, params);

        return json;
    }


    public boolean isUserLoggedIn(Context context){
        DatabaseHandler db= new DatabaseHandler(context);
        int count=db.getRowCount();
        if(count>0){
            return true;
        }
        return false;
    }


    public boolean logoutUser(Context context){
        DatabaseHandler db=new DatabaseHandler(context);
        db.resetTable();
        return false;   
    }
}

2.这是index.php

if(isset($_POST['tag'])&& $_POST['tag'] !=''){
        //get tag
        $tag=$_POST['tag'];

        //include DB handler
        require_once ('DB_Functions.php');//ganti include
        $db=new DB_Functions();

        //response array
        $response=array ("tag"=> $tag, "Success"=>0, "error"=>0);

        //check 4 tag type
        if($tag='login'){
            //req type is check login
            $email=$_POST['email'];
            $password=$_POST['password'];

            //check 4 user
            $user=$db->GetUserByEmailAndPassword($email, $password);
            if($user !=false){
                /* user found
                * echo JSON with Success =1 */
                $response ["Success"]=1;
                $response["uid"]=$user["unique_id"];
                $response ["user"]["name"]=$user["name"];
                $response["user"]["email"]=$user["email"];
                $response ["user"]["created_at"]=$user["created_at"];
                $response["user"]["updated_at"]=$user["updated_at"];
                echo json_encode($response);
            }
            else{
                /* user not found
                * echo JSON with error =1 */
                $response ["Error"]=1;
                $response["error_msg"]="INCORRECT EMAIL OR PASSWORD!!!";
                echo json_encode($response);
            }
        }
        else if($tag=='register'){
            //req type is register new user
            $name=$_POST['name'];
            $email=$_POST['email'];
            $password=$_POST['password'];

            //check if user already existed
            if($db->IsUserExisted($email)){
                //user is already existed - error response
                $response["error"]=2;
                $response["error_msg"]="USER IS ALREADY EXISTS";
                echo json_encode($response);
            }
            else{
                //store user
                $user=$db->StoreUser($name, $email, $password);
                if($user){
                    //user stored successfull
                    $response ["Success"]=1;
                    $response["uid"]=$user["unique_id"];
                    $response ["user"]["name"]=$user["name"];
                    $response["user"]["email"]=$user["email"];
                    $response ["user"]["created_at"]=$user["created_at"];
                    $response["user"]["updated_at"]=$user["updated_at"];
                    echo json_encode($response);
                }
                else{
                    //user failed to store
                    $response ["Error"]=1;
                    $response["error_msg"]="ERROR OCCURED IN REGISTRATION";
                    echo json_encode($response);
                }
            }
        }
        else{
            echo "INVALID REQUEST";
        }
    }
    else{
        echo "ACCESS DENIED";
    }
?>

<强>3。这是 DB_Functions.php

<?php
    class DB_Functions{
        private $db;
        /*constructor*/
        function __construct($db){
            require_once ('DB_Connect.php');//ganti include
            /*connecting DB*/
            $this->db= new DB_Connect();
            $this->db->connect();
        }
        /*destructor*/
        function __destruct($db){
        }

        /*
        *storing new user
        *returning user detail
        */

        public function StoreUser($name, $email, $password){
            $uuid= uniqid('',true);
            $hash= $this->hashSSHA($password);
            $encrypted_password= $hash["encrypted"]; //encripted password
            $salt=$hash["salt"];//salt
            $result=mysql_query("INSERT INTO account_users(unique_id, name, email, encrypted_password, salt, created_at) VALUES ('$uuid','$name','$email','$encrypted_password','$salt',NOW())");
            /*check for succesfull store*/
            if($result){
                /*get user detail*/
                $uid= mysql_insert_id();
                $result= mysql_query("SELECT * FROM account_users WHERE uid='$uid'");
                /*return user detail*/
                return mysql_fetch_array($result);
            }
            else{
                return false;
            }
        }
        /*
        *get user by email & password
        */

        public function GetUserByEmailAndPassword($email, $password){
            $result=mysql_query("SELECT * FROM account_users WHERE email='$email'") or die (mysql_error());
            //check 4 result
            $no_of_rows=mysql_num_rows($result);
            if($no_of_rows>0){
                $result=mysql_fetch_array($result);
                $salt=$result['salt'];
                $encrypted_password=$result['encrypted_password'];
                $hash=$this->checkhashSSHA($salt, $password);
                //check pass equality
                if($encrypted_password==$hash){
                    //user auth are correct
                    return $result;
                }
            }
            else{
                //user not found
                return false;
            }
        }

        /*
        *check user existed or not
        */
        public function IsUserExisted($email){
            $result=mysql_query("SELECT email FROM account_users WHERE email='$email'");
            $no_of_rows=mysql_num_rows($result);
            if($no_of_rows>0){
                //user existed
                return true;
            }
            else{
                //user not existed
                return false;
            }
        }
        /*
        *encrypting pass
        *@param pass
        *return salt & encrpted pass
        */
        public function hashSHHA($password){
            $salt=sha1(rand());
            $salt=substr($salt, 0, 10);
            $encrypted= base64_encode(sha1($password.$salt, true).$salt);
            $hash=array("salt"=> $salt, "encrypted"=> $encrypted);
            return $hash();
        }
        /*
        *decrypting pass
        *@param salt, pass
        *return hash string
        */
        public function CheckHashSSHA($salt, $password){
            $hash=base64_encode(sha1($password.$salt, true).$salt);
            return $hash;
        }

    }
?>

<强>4。最后这是我运行 android 代码时的错误

07-18 02:07:59.393: D/dalvikvm(541): GC_EXPLICIT freed 3420 objects / 314064 bytes in 340ms
07-18 02:08:08.802: E/JSON(541): <br />n<b>Fatal error</b>:  Destructor DB_Functions::__destruct() cannot take arguments in <b>C:\xampp\htdocs\android\include\DB_Functions.php</b> on line <b>13</b><br />n
07-18 02:08:08.802: E/JSON PARSER(541): Error parsing dataorg.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
07-18 02:08:08.802: W/System.err(541): java.lang.NullPointerException
07-18 02:08:08.812: W/System.err(541):  at com.androidTA.androidclient.RegisterActivity$1.onClick(RegisterActivity.java:58)
07-18 02:08:08.812: W/System.err(541):  at android.view.View.performClick(View.java:2408)
07-18 02:08:08.812: W/System.err(541):  at android.view.View$PerformClick.run(View.java:8816)
07-18 02:08:08.812: W/System.err(541):  at android.os.Handler.handleCallback(Handler.java:587)
07-18 02:08:08.812: W/System.err(541):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-18 02:08:08.812: W/System.err(541):  at android.os.Looper.loop(Looper.java:123)
07-18 02:08:08.812: W/System.err(541):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-18 02:08:08.822: W/System.err(541):  at java.lang.reflect.Method.invokeNative(Native Method)
07-18 02:08:08.858: W/System.err(541):  at java.lang.reflect.Method.invoke(Method.java:521)
07-18 02:08:08.858: W/System.err(541):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-18 02:08:08.858: W/System.err(541):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-18 02:08:08.862: W/System.err(541):  at dalvik.system.NativeStart.main(Native Method)

我的 destruct 函数有问题...,请任何人帮我解决这个问题...,非常感谢您的解决方案,,,:)

最佳答案

从构造函数/析构函数参数中删除所有 $db 参数。就像这样:

    function __construct(){
        require_once ('DB_Connect.php');//ganti include
        /*connecting DB*/
        $this->db= new DB_Connect();
        $this->db->connect();
    }

当您通过 $this->db 引用时,参数列表中不需要 $db,因此它不是必需的。

    function __destruct(){
    }

您不必传递任何其他参数,因为对象已全部清除

关于php - fatal error : Destructor cannot take arguments,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17708588/

相关文章:

mysql - `@` mysql变量名中的符号

php - Swift Poststring 没有得到我的值

php - 如何摆脱主题窃取者?

php - 如何简化 PHP 中的表单处理?

javascript - 我如何在 android 中使用 javascript 将 Edittext 值放入 webview 中的文本字段

mysql - 在mysql中为数据库中的每个表自动运行查询

mysql - 将mysql datetime解析为golang时间

php 文件自动更改为 php.suspected

android - 如何找出应用程序(Whats App)的使用时间?

android - isCoreLibraryDesugaringEnabled 在 gradle kotlin dsl/kts 中不起作用