I have 3 files. utils.php (contains function statusDefaultMessage()), userdetails.php (contains function insertOwner()), and insertCreateUser.php. I'd like to call statusDefaultMessage() in userdetails.php but I am unable to figure out how. I've omitted unnecessary code to avoid overcrowding, but below are the snippets of the code I am using.
// utils.php
<?php
class Utils{
// database connection and table name
private $conn;
// constructor with $db as database connection
public function __construct($db) {
$this->conn = $db;
}
// function to be called in another class
public function statusDefaultMessage($code, $msg) {
$data=array();
$data = array(
"response_code" => $code,
"status" => $msg
);
return $data;
}
}
?>
.
// userdetails.php
<?php
// include database and object files
include_once '../objects/utils.php';
class UserDetails{
// database connection and table name
private $conn;
private $userdetails_table = "userdetails";
// constructor with $db as database connection
public function __construct($db){
$this->conn = $db;
}
$utils = new Utils();
public function insertOwner($user_group_id, $name, $email_id, $enc_pass, $mobile_no, $company_id, $created_by, $status) {
$data=array();
if (($created_by==null && $status==0)) {
// query to insert record
$sql = "INSERT into ".$this->userdetails_table." (user_group_id, name, email_id, password, mobile_no, company_id) values(".$user_group_id.", '".$name."', '".$email_id."', '".$enc_pass."', '".$mobile_no."', '".$company_id."');";
if($this->conn->exec($sql)){
$data = $utils->statusDefaultMessage("1", "Signup successful!");
} else {
$data = $utils->statusDefaultMessage("0", "Signup failed!");
}
}
return $data;
}
}
?>
.
// insertCreateUser.php
<?php
// include database and object files
include_once '../config/database.php';
include_once '../objects/utils.php';
include_once '../objects/userdetails.php';
// instantiate database and product object
$database = new Database();
$db = $database->getConnection();
$utils = new Utils($db);
$userdetails = new UserDetails($db);
// assign necessary tables
$companies_table = "companies";
$company_name = isset($_POST['company_name']) ? $_POST['company_name']:"";
// Example of how I intend to use the functions from userdetails.php and utils.php
$sql = "INSERT into ".$companies_table." (company_name) values('".$company_name."');";
if($db->exec($sql)) {
$data = $userdetails->insertOwner($user_group_id, $name, $email_id, $enc_pass, $mobile_no, $company_id, $created_by, $status);
} else {
$data = $utils->statusDefaultMessage("0", "Company not inserted!");
}
//end
echo json_encode($data);
?>
I'm facing syntax error issues and it would be greatly appreciated if someone can guide me in the right direction. Again, I'd like to be able to use the function statusDefaultMessage() in userdetails.php. Thank you.
<br />
<b>Parse error</b>: syntax error, unexpected '$utils' (T_VARIABLE), expecting function (T_FUNCTION) in <b>C:\xampp\htdocs\esecurity_repo\objects\userdetails.php</b> on line <b>19</b><br />