can I use define inside of a class to use for function input. Like this:
PHP Code:
class DB{
define('ARRAY_A', 'associative');
define('ARRAY_N', 'numeric');
public function fetch($query, $type = ARRAY_N){
// do sql work here
}
}
$db = new DB('localhost', 'root', 'pass');
$db->fetch("select * from users", ARRAY_A);
or maybe like this
PHP Code:
class DB{
public function fetch($query, $type = ARRAY_N){
define('ARRAY_A', 'associative');
define('ARRAY_N', 'numeric');
// do sql work here
}
}
$db = new DB('localhost', 'root', 'pass');
Use a const, its better than a define since it has a scope limitation to it.
PHP Code:
class DB{
const ARRAY_A = 'associative';
const ARRAY_N = 'numeric';
public function fetch($query, $type = self::ARRAY_N){
// do sql work here
}
}
$db = new DB('localhost', 'root', 'pass');
$db->fetch("select * from users", DB::ARRAY_A);
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Not if its defined as a const you can't. Its a scoping issue, if you don't include the self:: or DB:: it doesn't know what you're looking for. Although its also a constant, its not a defined constant like the define() function which places it into a global scope.
If you really wanted it to, you should be able to use the value of a constant as a constant. I believe it only needs to be constant data (ie: not a variable), so you may be able to do this:
PHP Code:
define('ARRAY_N', 'associative');
class DB
{
const ARRAY_N = ARRAY_N;
}
It seems a little redundant though, and I can't be 100% certain if that works (I'm at work and can't test atm).
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php