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:


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


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?

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?

I guess in a roundabout way I am saying give Class Four a foo() method that does nothing, is another option.

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.

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.

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 ?