How to use PHP Objects globally, and include objects from multiple file inside a function of a class? -
assume, have 2 files:
dbconnect.php calculate.php
calculate has abstract class "authx" functions
login
,signup
,forgotpass
,newsdisplay
etc.to make functions work hav actual class "auth" above functions added in class file.
db connect.php has database related functionalities, has class file
now, need use objects of dbcon inside login, signup, forgotpass.etc., 1 way include settings in each of these functions again , again, works fine.
but alternative , better solution think assigning , using database objects great inside functions 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 use of json standard thing! *do error handling totally *make login, noback, signup, forgot password, deploy on server, **/ /** * connecting database **/ abstract class auth { /** *setting abstract classes login file * **/ abstract function login(); /** *developing sign module * **/ abstract function signup(); /** *forgot pass **/ abstract function forgotpass(); } /** *this class make login work **/ /** *make json based switch */ class authx extends auth { function jsonhandler(){ //data switch information } function login() { } function signup() { // $test = new authx(); // echo $test->forgotpass(); //wrong way code //$getname="chocolate"; $get_uname=$_get['signupname']; $get_uemail=$_get['signupemail']; $get_upwd=$_get['signuppwd']; //$get_uemail=$_get['uemail']; echo "variable ".$getname. "<br/>"; //$sql = "insert `tb_users`(`u_id`, `u_name`, `u_email`, `u_pwd`) values (null,\"hello\",\"hello\",\"hey\")"; //$sql="insert names(name,email) values('$get_uname','$get_uemail')"; $sql="insert 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 recommend following standard structuring code , using autoloading. check out link: http://www.sitepoint.com/autoloading-and-the-psr-0-standard/.
Comments
Post a Comment