On Saturday 26 September 2009 06:57 PM, PHPScriptor wrote:
Hello there,

I worked for different companies and saw different ways of programming
websites.
Now everybody is talking about OO programming. But my question is: why?

Let me give you an example one of the ways I saw it done:

a class member

Class Member {
        protected $_id;
        protected $_name;
        portected $_street;
        ...

        public function setId($id) {
                $this->_id = $id;
        }
        public function getId() {
                return $this->_id;
        }
        ...
        public function save() {
                $member = array('name' =>  $this->getId(), 'street', 
$this->getStreet(),
...)
                $db->insert($member);
        }
}

When a form is posted it did:
        $member = new Member();
        $member->setName($this->_getParam('member')
                ->setStreet($this->_getParam('street');
        ...
        $member->save();

Also when you get data from the db it will be inserted in the object, and
get from the object when you need to show it (eg in the view).


Another way (I did it) before:

Just in the controller after eg posting a form:
        $data = $this->_getAllParams();
        $db->insert($data);

Like you see, my way is a lot shorter :-).
I think you are confusing what the code snippet is trying to demonstrate.

Well, if you use Zend_Form you could do something like
$data = $form->getValues();
$data->insert($data);

All the form fields would be filtered and validated as well. Even more better than your snippet? Think SQL injection.

But comments I get is: it's not
really OO. They are true with that comment, but why should you make all
those object, all those set en gets, ... Why isn't it better to work with
arrays?
With the Zend framework IMHO you don't need to configurate all those
objects. Ok, in some systems it's better. But let's talk about
administration application, where you only have forms to input or edit,
delete and view lots of data.

What 's your meaning, why is it better to use them? ...

It is next to impossible to write all the benefits of using OOP in this email. You should read books about OOP to learn more about it.

The sample you snippet posted appears to demonstrate how to encapsulate database access in your models.

--

With warm regards,
Sudheer. S
Business: http://binaryvibes.co.in, Tech stuff: http://techchorus.net, 
Personal: http://sudheer.net

Reply via email to