-2

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 />
tereško
  • 58,060
  • 25
  • 98
  • 150
Abd Maj
  • 197
  • 1
  • 13

3 Answers3

2

I'm not sure if this is the syntax error you're running into, but you can't have this

$utils = new Utils();

In the middle of your UserDetails class. It has to be inside the insertOwner method.

Mr Glass
  • 1,186
  • 1
  • 6
  • 14
1

In userdetails.php, you are trying to use $utils inside your function insertOwner() but it does not have scope in there.

Try putting this line within the function $utils = new Utils();

Andrew Chart
  • 623
  • 6
  • 10
1

As mentioned, you can't declare $utils = new Utils(); in the middle of your class (hence the error you're getting).

Based on your code, it looks like the function you're calling doesn't depend on any internal state of the Utils object, so in that case you can just make it a static function (so no need to create an instance of it).

// Declaring it in your Utils class:
class Utils {

    // <other code>

    public static function statusDefaultMessage($code, $msg) { ... }
}


// Calling it in your user details class (or anywhere, really):
Utils::statusDefaultMessage('foo', 'bar');

If you do need to access the internal state of the Utils object (maybe for a different method), then you should probably pass that instance in as a variable, either to the constructor (same way you're doing with $db now) or as a parameter to the function that needs to use it.

Mike B.
  • 1,422
  • 12
  • 8