Jump to content

Passing an object to a class method?


jasonwisdom

Recommended Posts

Can PHP pass an object to a class method?

 

I have a $mysqli object, representing the mysqli DB class of course.

 

I am replacing mysql_pconnect() which used to exist at the top of every page.

 

My classes just used the mysql_query() methods and similar....I want/need to replace these with $mysqli->multi_query().

 

But when I simply use $mysqli->multi_query(), I get an error.  Fair enough...so I pass "$mysqli" as a parameter to the public methods. 

Now I don't get an error, but no actual results get returned.

 

Any thoughts?  Let me know if you want me to clarify.

 

Thanks a bunch,

Jason

 

Link to comment
Share on other sites

This code is extremely stripped down.......

 

 

class User {

 

public $userid;

public $access;

 

function __construct()

{

$this->ResetValues();

}

 

private function ResetValues()

{

$this->userid = 0;

$this->access=$this->InitAccess();

}

 

private function InitAccess()

{

$access = new UserAccess();

return $access;

}

 

public function LoadUser($userid, $mysqli)

{

echo "Now in load user.<br>";

$this->userid=$userid;

$this->access->LoadAccess($userid,$mysqli);

}

}

 

 

class UserAccess {

 

public $hasuser;

 

function __construct()

{

$this->hasuser=0;

}

 

public function LoadAccess($userid, $mysqli)

{

echo "Now in load access.<br>";

$sql="CALL spSelectUserAccess(".$userid.")";

 

if ($mysqli->multi_query($sql))

{

echo "Now in SQL loop.<br>";

if($result = $mysqli->store_result())

{

while($row = $result->fetch_array(MYSQLI_ASSOC))

{

if($row['access']=="User")

{$this->hasuser=1;}

}

}

}

}

}

 

 

 

When I run this.....

 

I see "Now in load user."

I see "Now in load access."

I do not see "Now in SQL loop."

 

Any ideas?

Hope this is clear.

 

Jason

 

Link to comment
Share on other sites

Chances are, the problem is that you are passing a resource by value, although your method of testing this is poor.  For example, you're looking to discern something about how PHP oop works, based on the return value of a query that could fail.

 

With that said, you might try this:  public function LoadAccess($userid, &$mysqli)

 

 

Link to comment
Share on other sites

Passing objects without using the & (reference) symbol should work fine in php5+. So i doubt the issue lies here.

Are you actually passing in the mysqli object?

 

I see no code showing how you are calling these object methods, and thus i'm not sure if you're actually doing

$user = new User();

$user->LoadUser(1, new MySqli("localhost", "my_user", "my_password", "world"));

 

I'm assuming you're doing something like the above, but until I see it i can't rule out the fact that you might not even be giving it a valid mysqli object.

 

According to the manual, mysqli->multi_query() returns a boolean result. Meaning that you're getting back FALSE from that query. So perhaps that user doesn't exist? Or the query is failling. I'm also guessing that stored procedures work ok in mysqli?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.