SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    which way would be fast and good???

    i am creating a class for error handling...
    just doing some customized things. i was wondering from the following which would be fast::
    1. using direct variables whereever i need them OR
    2. using getter functions for each and every variable

    THE GOOD PART i found in the second one is, if i want to change the variable names later on then i can change them easily

    THE WORRIED PART is, i think it gonna make it slower.

    what would you suggest which way i would be fast and better and which way i should choose

    thank you

  2. #2
    Web Design Ireland cianuro's Avatar
    Join Date
    Jun 2004
    Location
    Dublin Ireland
    Posts
    914
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, it depends on how big your application is going to be.

    How many variables are you going to need?

  3. #3
    SitePoint Addict pointbeing's Avatar
    Join Date
    Jun 2004
    Location
    London, UK
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jaswinder_rana
    1. using direct variables whereever i need them OR
    2. using getter functions for each and every variable
    Your class variables should always be private. So don't choose 1.

    As you mention, that means you can change the names of the variables. Indeed, you can change the whole entire way the class works in the future, as long as the interface remains the same. That's part of the beauty of OO.

    Now bear in mind, if you think carefully about your class, and how it encapsulates its own data and behaviour, you'll find that you don't need to expose "each and every variable" anyway. So there's no justification for blindly creating a list of getters and setters. Just a select few. Again. it comes down to thinking about the class's interface.

    If you're worried about speed, then you can stop worrying - if there is a difference, it will be so small as to be completely unnoticeable.

  4. #4
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Now bear in mind, if you think carefully about your class, and how it encapsulates its own data and behaviour, you'll find that you don't need to expose "each and every variable" anyway. So there's no justification for blindly creating a list of getters and setters. Just a select few. Again. it comes down to thinking about the class's interface.
    i am not going to use these get functions in the script that will use that class. what i meant was, i would use those get functions inside the class. say, if i use a function inclass as dump_array() and i need to check if a flag say $debug_flag is on. then in that class function instead of using $this->debug_flag i am using $this->get_debug_flag() and in that function i am returning the variable.

    so, i am not sure whther this approach would lead to a slower response time or not

    and as pointbeing i think the same thing that time difference will be much less

  5. #5
    SitePoint Addict pointbeing's Avatar
    Join Date
    Jun 2004
    Location
    London, UK
    Posts
    227
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh right - I misunderstood your question completely!

    If you're accessing the variables from within the class, then just use them, that's what they're there for. I can't see any reason for creating accessors which are only intended for use by the class, and I can see numerous reasons not to. If nothing else, these would include i) a horrendously confused class interface ii) stacks of unnecessary code to maintain.

    Just my two cents, maybe others have other feelings about it.

  6. #6
    Umm. PHP Guru....Naaaah jaswinder_rana's Avatar
    Join Date
    Jul 2004
    Location
    canada
    Posts
    3,193
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    personally i think it makes your code much more managemable, but i am not that experienced.

    [u]i would change that[/b]

    But, just cusrious. would using functions instead of direct variable names would slow down the script or not??


Bookmarks

Posting Permissions

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