#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    398
    Rep Power
    189

    Undefined class constant


    Is there a way to get a static function to read class variables?

    I've tried several variations of the following:
    PHP Code:
    class user {
        static 
    $db_table='ht_users';
        static 
    $id_field='userid';

       static function 
    testing() {
           echo 
    user::id_field ;
           echo 
    self::db_table;
       }
    }
    user::testing(); 
    Thomas Tremain
  2. #2
  3. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,232
    Rep Power
    9400
    Static variables need the $s.
    PHP Code:
    echo user::$id_field;
    echo 
    self::$db_table
    Without them PHP thinks you want a class constant. Which incidentally is probably more appropriate for those two than variables.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    398
    Rep Power
    189
    First off Requinix, I want to say thanks.. You seem to always be around, and very helpful. I try to come in here and help now and then but it seems like I almost only make it here when I can't find my answer with a search engine.


    I'd like to extend my question a little further now.

    Now I'd like to get the variable from the current class (or instance) but into an inherited method.

    I'd like $user->viewid() to give me the value "userid" from the current class, not the equivalent value from the parent class houses the viewit() method.

    PHP Code:
    class database {
        static 
    $id_field='dummy';

        public function 
    viewid() {
            echo 
    self::$id_field .'<br>';
        }
    }
    class 
    user extends database{
        static 
    $id_field='userid';

       static function 
    testing() {
           echo 
    self::$id_field .'<br>';
       }
    }

    $user= new user;
    $user->viewid();  //  Displays 'dummy' which is how I feel
    user::testing(); 
    Thomas Tremain
  6. #4
  7. Transforming Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,232
    Rep Power
    9400
    I'm always around because I have nothing better to do


    If you have PHP 5.3+ you can use the "static" keyword. Works the same way that "parent" and "self" do.
    PHP Code:
    static::$id_field 

    Comments on this post

    • ManiacDan agrees
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    398
    Rep Power
    189
    Originally Posted by requinix
    I'm always around because I have nothing better to do
    Well, we like having you around! (or at least I do)

    Over the last several years I have looked at, but never gotten a real grasp of OOP.

    I dove into PDO a couple of weeks ago, and finally started taking OOP seriously just over a week ago.

    I wish I'd done this earlier! PDO alone is saving HUGE amounts of my workload. Now I'm finally getting a hang of OOP and inheritance! It's going to make my current side project so much easier...

    I wish I had time to go back and fix some older projects...

    With mysql_* in the verge of being depreciated, I've run across two client boxes that don't even have PDO or mysqli support yet. Sigh, I guess that means I'll still have some mysql_* projects in my future...

    I'm rambling... Anyway, Thanks again!
    Thomas Tremain
  10. #6
  11. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,908
    Rep Power
    6352
    I wish I'd done this earlier! PDO alone is saving HUGE amounts of my workload. Now I'm finally getting a hang of OOP and inheritance! It's going to make my current side project so much easier...
    I'm going to try to remember to link people to this thread next time someone says OOP has no benefit (like if Arugula ever comes back)
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2005
    Location
    Vancouver, WA, USA
    Posts
    398
    Rep Power
    189
    Originally Posted by ManiacDan
    I'm going to try to remember to link people to this thread next time someone says OOP has no benefit (like if Arugula ever comes back)
    Well, once you simply make sure magic_quotes hasn't tried escaping anything, from there everything, you are done escaping and unescaping PERIOD! (Okay, if you really think you need to escape manually, there is the quote method...)

    The only thing left is htmlizing your output...

    I'm sure, more than once I've said.. "I'm in the admin area of my own site, there's nobody in here but me, I don't need to escape that one... or maybe I'll get back to it..." (Hello my name is Tom, and I've written insecure/poor code)

    PDO saves a LOT of time.. The extra time it takes to prepare and execute, is nothing compared to the time it used to take escaping and unescaping data (and remembering what to escape and what not to)

    In my case, I just created a helper function (or later a method) for the simple queries that takes the query, and an array of values (or even a single value.) Then a few more helper functions to resemble other methods I was used to, and presto.

    I'm spending a lot of time reorganizing the code for a side project I started about a month ago, but it's so much cleaner now, and I think it's much easier to manage.

    I had a little help with a series of video tutorials to get me past the OOP block I've had for years. I'm actually glad I waited for PHP 5.4 and it's inheritance features that I was just tackling last night.
    Thomas Tremain

IMN logo majestic logo threadwatch logo seochat tools logo