Reputation: 27048
i have a class:
class Connect
{
public function auth()
{
... do something
}
}
and i have a function: getfile.php
<?php
require_once($GLOBALS['path']."connect.php");
?>
the i have the: connect.php
<?php
function connect( $host, $database )
{
database connection here
}
?>
how can i use this functions inside my class like this:
class Connect
{
require_once("getfile.php");
public function auth()
{
connect( $host, $database )
... do query
}
}
is this possible?
thanks
Upvotes: 1
Views: 4785
Reputation: 15476
Functions declared in the global scope is available globally. So you don't have to include it where you need it, just include the file with the function in the beginning of your script.
Secondly,
class Connect
{
require_once("getfile.php");
public function auth()
{
connect( $host, $database )
... do query
}
}
this is just jibberish; you can't execute something inside the class outside of methods. If you really just want something included ONLY when that specific file is needed in that specific method, do something like this:
class Connect
{
public function auth()
{
require_once("getfile.php");
connect( $host, $database )
... do query
}
}
Upvotes: 2
Reputation: 179136
You can't add functions to a class in that manner.
If you're trying to add mixins
to a class, you should read this.
Otherwise you should stick to standard OOP practices by making a base class (or abstract class) and extend it:
class Connect extends Connector
{
...
}
class Connector
{
public function connect($host, $database) {
...
}
}
Upvotes: 3