PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Jan 21, 2020 12:50 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: Sun Mar 08, 2015 5:47 pm 
Offline
Forum Newbie

Joined: Sun Mar 08, 2015 5:08 pm
Posts: 3
I know basics of OOP and I use it for every project including more than about 100 lines of code. However I have a nagging question.

If I have a class that accepts some data through its public method(s) and then does something with/to that data with protected/private methods, is it better to use class properties, or pass data around as method arguments? For example:

Syntax: [ Download ] [ Hide ]
class Example{
  private $_foo;
  private $_bar;

  public function insert($foo,$bar){
    $this->_foo = $foo;
    $this->_bar = $bar;

    if($this->check()){
      //insert into DB or do some other things
    }
  }

  protected function check(){
    //some validation code, return true or false
  }
}


Would it be better instead to pass the data to the check() method as arguments without creating class properties, like this:

Syntax: [ Download ] [ Hide ]
class Example{

  public function insert($foo,$bar){

    if($this->check($foo,$bar)){
      //insert into DB or do some other things
    }
  }

  private function check($foo,$bar){}
}


As far as I can say, not creating additional variables would decrease memory usage slightly (or am I wrong), but on larger projects this kind of code would get too cumbersome, at least for me. On the other hand, for some simpler things, the latter approach seems more elegant because there's less code involved.

So, what can you tell me about advantages and disadvantages of these two approaches.

Thanks in advance.


Top
 Profile  
 
PostPosted: Sun Mar 08, 2015 6:30 pm 
Offline
Spammer :|
User avatar

Joined: Wed Oct 15, 2008 2:35 am
Posts: 6617
Location: WA, USA
Forget memory usage. It's just not that important.

In general the first approach is better. It's much easier to work with. With the second the calling code has to track multiple variables that it doesn't necessarily care about, and it's basically just a bunch of glorified functions. Not OOP.

Imagine a more complicated scenario:
Syntax: [ Download ] [ Hide ]
class User {

        public function insert($firstname, $lastname, $email, $address, $city, $state, $zip, $country) {
                //
        }

        public function update($id, $firstname, $lastname, $email, $address, $city, $state, $zip, $country) {
                //
        }

        public function validate($firstname, $lastname, $email, $address, $city, $state, $zip, $country) {
                //
        }

}

Do you really want to have to work with that? An alternative is
Syntax: [ Download ] [ Hide ]
class User {

        private $id;
        private $firstname;
        private $lastname;
        private $email;
        private $address;
        private $city;
        private $state;
        private $zip;
        private $country;

        public function __construct() {
                //
        }

        public function setAddress($address, $city, $state, $zip, $country) {
                $this->address = $address;
                $this->city = $city;
                $this->state = $state;
                $this->zip = $zip;
                $this->country = $country;
        }

        public function setName($firstname, $lastname) {
                $this->firstname = $firstname;
                $this->lastname = $lastname;
        }

        public function save() {
                if ($this->id) {
                        // insert
                } else {
                        // update
                }
        }

        public function validate() {
                //
        }

}


Top
 Profile  
 
PostPosted: Mon Mar 09, 2015 3:01 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
PostPosted: Tue Mar 10, 2015 5:54 pm 
Offline
Forum Newbie

Joined: Sun Mar 08, 2015 5:08 pm
Posts: 3


Top
 Profile  
 
PostPosted: Tue Mar 10, 2015 7:29 pm 
Offline
Moderator
User avatar

Joined: Tue Nov 09, 2010 3:39 pm
Posts: 6425
Location: Montreal, Canada
Your classes shouldn't have includes in them, and you shouldn't use the global keyword, well, ever really. If your class needs access to something, pass it in. into your objects, don't pull them in.

_________________


Top
 Profile  
 
PostPosted: Tue Mar 10, 2015 11:18 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
PostPosted: Wed Apr 08, 2015 4:29 am 
Offline
Forum Newbie

Joined: Mon Feb 15, 2010 7:01 am
Posts: 16
<Christopher>
"PS - I'd recommend protected over private, unless you absolutely know that you want to forbid a child class from accessing a property.:

I agree with this adding “directly” after the end of the sentence. Remember bicycle :) for example gear is something that all bicycles have, but should be private in bicycle because it might need to do something more when someone sets the gear (when you create those setters and getters probably no one do anything but you don't know in future). So a mountainBike shouldn't change the gear itself but rather set the gear, if this specific bicycle wants to have a more composite functionality when the gear is set then it should override its parent setGear() and preferably call it and then do its own things.


Top
 Profile  
 
PostPosted: Wed Apr 08, 2015 11:18 pm 
Offline
Site Administrator
User avatar

Joined: Wed Aug 25, 2004 7:54 pm
Posts: 13592
Location: New York, NY, US

_________________
(#10850)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 7 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group