SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Canberra, Australia
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Accessor functions and data types

    Hi all,

    Here's a quick question... is it a good idea to enforce the type of value that can be stored in a particular object property or attribute? i.e. the setter function forces a passed value to be an integer or a string, or perhaps verifies that the value is a particular kind of class. For example:

    PHP Code:
    public function setSomething($something)
    {
       
    $this->something = (string) $something;

    Is this good practice or overkill? Opinions?

    Great forums by the way, glad I found them!

    Cheers,

    {R}

  2. #2
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not usually necessary in php it would be rare if the code that accepts the return value was type-sensitive. You might want to force boolean type occasionally to avoid ambiguity with 0's & etc but that's not always necessary.

  3. #3
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I find it is not necessary to enforce type of scalars, but I often check if the value is not a scalar and deal with it appropriately.

  4. #4
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I occasionally use it. casts can look a little messy though.

    I do find it handy for SQL. Many people use stupid crazy escaping methods for all of their SQL sanity checks. Often you are refering/addressing rows in your database by a primary key, or some relationship that is bound together by numeric data. In this case I like do something like ...

    $sql = 'SELECT stuff FROM rubbish WHERE scank_id = '.(int)$scanky_id;

    This is a nice easy way to remove injections. Some ppl in other languages do all sorts of crap like ...

    $sql = 'SELECT stuff FROM rubbish WHERE scank_id = '. 0 + $scanky_id;

    which I find confusing.

    The other place when it is handy is when you wanna pass a parameter that is an array list for >1 item, but only use a string if there is only one thing in the list ...

    function getRubbishByScankIds($ids) {
    $ids = (array)$ids;
    // Below I assume all the array elelements are all numbers and are from a safe datasource
    $sql = 'SELECT stuff FROM rubbish WHERE scank_id IN ('.implode(', ', $ids).')';
    }

    as you can see this is a good way to cast strings as arrays when you are mixing strings and arrays as paramters. not many ppl know about the (array) cast (in fact I'm the only person I know who uses it!)

    I only use casts where I need them. Don't use them everywhere, as it's not needed and will only slow stuff down.

  5. #5
    SitePoint Enthusiast
    Join Date
    Nov 2004
    Location
    Canberra, Australia
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys for your feedback! Much appreciated... these forums rock btw, best I've seen for 'serious' PHP programming by a long mile. ;D


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
  •