Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts

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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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); 
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #3
    Regular Coder ninnypants's Avatar
    Join Date
    Apr 2008
    Location
    Utah
    Posts
    504
    Thanks
    10
    Thanked 47 Times in 47 Posts
    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); 

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    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).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •