in PHP DEV ~ read.

Bind Class from Form in PHP

Recently I had my first experience with PHP.
Until then, I had only seen how PHP systems works.

The problem is the following, I needed to implement the CRUD, It's was about ten class and I did not want to implement for all the same code that get request values and set in each property.
So, I remembered the Bind Class from Form. After of some searchs, I did find this method:

    function PopulateWithRequest($obj = NULL) {
        if (!is_object($obj)) {
            $obj = new StdClass ();
        }

        foreach ($_REQUEST as $var => $value) {
            //Set Value
            $obj->$var = trim($value); //here you can add a filter, like htmlentities ...
        }

        return $obj;
    }



I put this method in base class and after I used like this:

    $entity = new Entity();
    $entity = $entity->PopulateWithRequest($entity);



I know that today exists Zend Framework, CodeIgniter, Laravel, CakePHP and several others, but the system that I was working was old and does not compensate reimplement everythink.

comments powered by Disqus