SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,816
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)

    Which would be the best practice?

    I have an object ( the s say it is of Class ONE), which has an array ( serving much like a registry) that contains objects of a few different other classes ( say: class TWO, class Three, class Four, and class Five). I KNOW class Two, Three and Five have the method foo() and that class Four does not.

    I have a function in class ONE which iterates through the registry calling foo(); so i have been pondering the best way to handle this and have come up with two ways of doing this:


    the HARD WIRED WAY:
    Code:
    foreach ($this->registry as $obj){
         $ndl=get_class($obj);
        if ($nd != 'Four'){ $obj->foo(); }
        /** I can also do   
             if ($ndl=='Two' || $ndl=='Three' || $ndl=='Five'){ $obj->foo(); }
       **/
    }
    OR

    Code:
    foreach ($this->registry as $obj){
        if ( method_exists($obj,'foo' )){ $obj->foo(); }
    }
    I am leaning towards the latter because it means i could expand the type of classes that i can put into the registry without having to alter my code. But I am concerned that it might be MUCH slower than merely getting the objects class.

    which do you the PHP experts consider best practice?
    Last edited by dresden_phoenix; Mar 8, 2013 at 19:12.

  2. #2
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    It sounds as if you are building a family of classes which closely resemble each other - is that true?

    If so I'd take a look at the Strategy Pattern as a way of managing these classes, and if that does not fit your bill, can you explain why?

  3. #3
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    I guess in a roundabout way I am saying give Class Four a foo() method that does nothing, is another option.

  4. #4
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,816
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    you are correct, in assuming the classes resemble each other.

    in fact each object represent an instance form control element being validated. obviously, classes representing control elements such as buttons, selects, radio buttons, check boxes, hidden inputs do not have any validation routines at all. just adding foo(){ return false;} I was just trying to be as DRY as possible.

  5. #5
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,184
    Mentioned
    17 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by dresden_phoenix
    in fact each object represent an instance form control element being validated. obviously, classes representing control elements such as buttons, selects, radio buttons, check boxes, hidden inputs do not have any validation routines at all. just adding foo(){ return false;} I was just trying to be as DRY as possible.
    How so?

    What I guess I'm saying is that there should be server side validation for things like radio groups, select menus and checkboxes to make sure an expected value is passed. A good example is a value for a select menu could be passed using cURL or some other means that does not match what in the list. Therefore, it would be smart to make sure any submitted values actually exist for the select menu. the same holds true for radio groups and checkboxes. Nothing prevents a user from making a GET or POST request without using the UI.

    Anyway, like Cups pretty much said you should probably be using an interface. Each form component should probably implement some interface for validation. If the form element truly doesn't need to be validated than it can just return false or whatever. I think though for this you should really consider programming to an interface. Also, I don't really think it should be responsibility of a form component to validate itself. There are so many different levels of validation and edge cases to consider that it is probably best to use a separate group of class for different types of validation or something.

    Both methods you suggested are no very OOP like. I guess that is what I'm saying. They could work but the more intelligent way of doing things (I guess depending on perspective) would be programming to an interface. Especially if you are trying to build some type of form API here.
    The only code I hate more than my own is everyone else's.

  6. #6
    Community Advisor silver trophybronze trophy
    dresden_phoenix's Avatar
    Join Date
    Jun 2008
    Location
    Madison, WI
    Posts
    2,816
    Mentioned
    34 Post(s)
    Tagged
    2 Thread(s)
    What I guess I'm saying is that there should be server side validation for things like radio groups, select menus and checkboxes to make sure an expected value is passed. A good example is a value for a select menu could be passed using cURL or some other means that does not match what in the list. Therefore, it would be smart to make sure any submitted values actually exist for the select menu. the same holds true for radio groups and checkboxes. Nothing prevents a user from making a GET or POST request without using the UI.
    good point . I was attempting to create a "framework"? in which a user could code the form elements as objects, inserting arrays of regex.. so I guess I was cutting corners I thought users would code.

    Both methods you suggested are no very OOP like.


    Somehow I think my concept was misunderstood.

    The registry also stores instances of NON-control elements ( say for example if the user wanted a H2 or a list of instructions). I am iterating an array of instance of VARIED, but similar, CLASSES. They are NOT child classes (so far). I want to call a method in the instances of certain classes ( specifically classes that contain the desired method, a validation routine ).

    So for what i understand from the above posts is that me best practice would be to REQUIRE/INCLUDE that method as part of all classes that could possibly end up in that registry ?
    Last edited by dresden_phoenix; Mar 9, 2013 at 22:23.


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
  •