/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] can’t pass instance of object

This issue is so strange:

I have a database class file, then I create an instance of this class:

[CODE]$database = new database($host, $user, $pwd, $dbName)[/CODE]

Then I use it to make some query and it works

[CODE]
$database->setQuery(“SELECT * FROM my_example limit 0,1”);
$company = $database->loadObjectList();
[/CODE]

Afte that, I want to pass this instance as one of my arguments into another class constructor:

[CODE]
require_once(“../classInfo.class.php”);
$objClass = new classInfo($courseID, $requestArr, $database);
[/CODE]

Inside of my “classInfo”, I have constructor:

[CODE]
$this->database = $database;
[/CODE]

But when I call a function inside of “classInfo”, it gives me “Call to a member function setQuery() on a non-object” error:

[CODE]
$objClass->callMyFunction();
[/CODE]

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@MrCoderJun 13.2007 — Can you show us the classes?
Copy linkTweet thisAlerts:
@sunnysideauthorJun 13.2007 — 
  • 1. database.php
    [CODE]
    class database {
    function database( $host='localhost', $user, $pass, $db='', $table_prefix='', $goOffline=true ) {
    .....
    }

    // and some other functions about db connections/functions
    }
    [/CODE]


  • 2. clsssInfo.class.php
    [CODE]
    class classInfo{
    private $database;

    public function __construct($myDatabase=null) {

    $this->database = $myDatabase;
    }

    public function createEvaluation() {

    $sql_portalLinkDev = "SELECT * FROM tbl_portal_course_link WHERE portalCourseId = '$this->portalCourseId' ORDER BY id ASC";
    $this->database->setQuery($sql_portalLinkDev);
    $result_portalLinkDev = $this->database->loadObjectList();
    }
    [/CODE]


  • 3. testing.php
    [CODE]
    $database = new database( $mosConfig_host, $mosConfig_user, $mosConfig_password, $mosConfig_db, $mosConfig_dbprefix );
    $database->setQuery("SELECT company FROM tbl_companies limit 0,1");
    $company = $database->loadObjectList();
    $company = $company[0]->company;

    require_once("../classInfo.class.php");
    $objClass = new classInfo($database);
    $objClass->createEvaluation(); // The error goes here, said "Call to a member function setQuery() on a non-object"
    [/CODE]
  • Copy linkTweet thisAlerts:
    @MrCoderJun 13.2007 — [code=php]
    <?php
    class database
    {
    public function setQuery()
    {
    echo "Database->setQuery()<br>";
    }
    }

    class classInfo
    {
    private $database;

    public function __construct($database)
    {
    $this->database = $database;
    }

    public function createEvaluation()
    {
    echo "classInfo->createEvaluation()<br>";

    $this->database->setQuery();
    }
    }

    $database = new database();
    $classInfo = new classInfo($database);
    $classInfo->createEvaluation();
    ?>
    [/code]


    This worked ok for me, but the following gave the same error as you were having..

    [code=php]
    <?php
    class database
    {
    public function setQuery()
    {
    echo "Database->setQuery()<br>";
    }
    }

    class classInfo
    {
    private $database;

    public function __construct($database)
    {
    //$this->database = $database;
    }

    public function createEvaluation()
    {
    echo "classInfo->createEvaluation()<br>";

    $this->database->setQuery();
    }
    }

    $database = new database();
    $classInfo = new classInfo($database);
    $classInfo->createEvaluation();
    ?>
    [/code]


    [CODE]
    Fatal error: Call to a member function setQuery() on a non-object
    [/CODE]


    As you can see, it looks like the $database is not a valid object(class) in your code.
    Copy linkTweet thisAlerts:
    @sunnysideauthorJun 13.2007 — Thank you for your helping, MrCoder. I finally found out the solution: inside of my createEvaluation(), I called another function which uses the db connection by $database->, but not the $this->dabatase->

    That caused this issue.

    Sorry, My bad! :-(
    Copy linkTweet thisAlerts:
    @MrCoderJun 13.2007 — I wish dreamweaver had an autocomplete system like Visual Studio ?

    Glad I could help.
    ×

    Success!

    Help @sunnyside spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...