简体   繁体   中英

How to use PHP Objects globally, and include objects from multiple file inside a function of a class?

Assume, I have 2 files:

dbconnect.php
calculate.php
  • calculate has abstract class "authx" with functions login , signup , forgotpass , newsDisplay etc.

  • To make functions work I hav actual class "auth" with above functions added in the class file.

  • DB connect.php has all the Database related functionalities, It has class file

  • Now, I need to use Objects of DBCON inside Login, Signup, forgotpass.Etc., One way to do this was to Include settings in each of these functions again and again, which works fine.

But as an alternative and better solution I think assigning and using database objects would be great inside the functions for better code flow.

dbcon.php

<?php
class dbcon{
    function connect(){
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname="work_steelnext";
        // Create connection
        $conn = new mysqli($servername, $username, $password,$dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    else{
        echo "Connected Successfully";
    }
    }
    function disconnect(){
        mysqli_close($conn);
    }
}   
?>

Calculate.php

<?php
include 'dbCon.php';
/**
* Make the use of JSON a standard thing!
*Do error Handling totally
*Make the Login, Noback, Signup, forgot Password, Deploy it on server, 
**/
/**
* Connecting the Database
**/

abstract class auth
{
    /**
    *Setting the Abstract classes for the Login file
    *
    **/

    abstract function login();
    /** 
    *Developing the Sign Up Module
    *
    **/
    abstract function signup();
    /**
    *forgot Pass
    **/
    abstract function forgotpass();
}
/**
*This class will make the login work
**/

/**
*Make a JSON Based Switch
*/

class authx extends auth
{

    function jsonhandler(){
        //Data Switch for the information
    }
    function login()
    {
    }
    function signup()
    {
    // $test = new authx();
    // echo $test->forgotpass();
    //Wrong way to code is 
    //$getname="chocolate";
    $get_uname=$_GET['signupName'];
    $get_uemail=$_GET['signupEmail'];
    $get_upwd=$_GET['signupPwd'];

    //$get_uemail=$_GET['uemail'];
    echo "Variable  ".$getname. "<br/>";
    //$sql = "INSERT INTO `tb_users`(`u_id`, `u_name`, `u_email`, `u_pwd`) VALUES (null,\"hello\",\"hello\",\"hey\")";
    //$sql="INSERT INTO names(name,email) VALUES('$get_uname','$get_uemail')";
    $sql="INSERT INTO tb_users(u_id,u_name,u_email,u_pwd) VALUES(null,'$get_uname','$get_uemail','$get_upwd')";

    $dblink=new dbCon();
    echo $dblink->connect();
    $conn="x";
    if (mysqli_query($conn, $   sql)) {
        echo "New record created successfully";
        echo $get_uname."Stored".$get_uemail;
    } else {
        echo "Error: " . $sql . "<br>" . mysqli_error($conn);
    }
    mysqli_close($conn);
    }

    function forgotpass()
    {
    }

    function switchandle($a)
    {
        switch($a)
        {
            case 'login':
            echo "\nlogin entered";
            break;
            case 'signup':
            echo "\nsigningup entered";
            break;
            case 'forgot':
            echo "\nI forgot password";
            break;
            default :
            echo "\nNull";
            break;
        }
    }
}

$test = new authx();
// echo $test->switchandle("login");
echo $test->signup();
?>

I strongly recommend following a standard for structuring your code and using autoloading. Check out this link: http://www.sitepoint.com/autoloading-and-the-psr-0-standard/ .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM