Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-17-2009, 04:16 AM   PM User | #1
ninnypants
Regular Coder

 
ninnypants's Avatar
 
Join Date: Apr 2008
Location: Utah
Posts: 504
Thanks: 10
Thanked 47 Times in 47 Posts
ninnypants is an unknown quantity at this point
defining variables inside a class

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');

$db->fetch("select * from users"ARRAY_A); 
ninnypants is offline   Reply With Quote
Old 03-17-2009, 04:22 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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
Fou-Lu is offline   Reply With Quote
Old 03-17-2009, 04:42 AM   PM User | #3
ninnypants
Regular Coder

 
ninnypants's Avatar
 
Join Date: Apr 2008
Location: Utah
Posts: 504
Thanks: 10
Thanked 47 Times in 47 Posts
ninnypants is an unknown quantity at this point
Just curious would it be possible to just use the constant without self:: or DB::
PHP Code:
class DB
    const 
ARRAY_A 'associative'
    const 
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); 
ninnypants is offline   Reply With Quote
Old 03-17-2009, 04:48 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:46 AM.


Advertisement
Log in to turn off these ads.