Jump to content

How to load a PHP class into a PHP functions


blmg2009

Recommended Posts

Hi, 

 

I'm struggling to understand how to load a PHP classes for my database into a php function. 

 

My class in on another file, which is included into my page. 

 

I then have a function that usings the class of $database->query in the function, but for some reason this doesn't work. 

 

However the class works on the page outside of the function, is there someway I must load the class into my function?

 

Thanks for reading. 

Link to comment
Share on other sites

WHAT is that word 'Database' doing in that header?

 

You need to pass the variable that represents your current instance of database class.  So

 

test(!one,$two,$three,$database)

 

is what you want.

 

IMHO - if you are such a newcomer to PHP, why are you getting into classes and whatever else (objects?) when you don't even know how to pass arguments to a function or how to format the header of one?  That is a basic premise of functions in any language that supports functions and you don't understand it.

Link to comment
Share on other sites

include_once"database.php";
 
function test($one, $two, $three, Database $database)
{
   $database->query(...); /// doesn't work inside this function
}
 
$database->query(...); /// works outside the function

 

 

So, some debugging 101 here.. you got an error that was essentially the result the variable not being within the scope of the function. So you updated your function declaration and now you are getting another error, which basically means you aren't passing to test what it expects.  To me, this sounds like you likely essentially have the same issue before, except backing up the chain a notch.  

 

So, where/how are you calling test()?  Did you update where you are calling it to have it actually pass a Database type object when you call it?  If so, are you calling test() within some other function, and $database isn't exposed to it, in the same way it wasn't exposed here?

 

Follow the trail, make sure $database is actually exposed, same as you already did here. 

Link to comment
Share on other sites

if $database is defined in the db file you could just call it as a global in the function like 

function test($one, $two, $three)
{
   global $database;
   $database->query(...); /// doesn't work inside this function
}

some people dont like to do this but it does work

Link to comment
Share on other sites

@blacknight, it's not that people don't like it, it's that it produces bad code that's harder to maintain because it requires that the programmer keep track of a greater amount of information, making his job harder and making it harder for anyone in the future to make use of the code or make changes to the code.

 

using global to being a value into a function breaks the black-box model of writing functions. the only interaction a function should have with the calling code is at the point where the function gets called.

  • Like 1
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.