Jump to content

Recommended Posts

Hello All,

 

I have a quick question on getters and setters in the below classes.  The classes below are exactly the same, return the same values, but named different methods?  Now are these examples both an examples of getter and setter methods?

 

<?php

class Box { 
  private $contents; 

  public function setContents($contents) { 
    $this->contents = $contents; 
  } 

  public function getContents() { 
    return $this->contents; 
  } 

} 

$box01 = new Box; 
$box01->setContents('I am contents of box01!'); 
echo $box01->getContents(); // Output: I am contents of box01! 


?> 

 

 

<?php

class Box { 
  private $contents; 

  public function produceContents($contents) { 
    $this->contents = $contents; 
  } 

  public function outputContents() { 
    return $this->contents; 
  } 

} 

$box01 = new Box; 
$box01->produceContents('I am contents of box01!'); 
echo $box01->outputContents(); // Output: I am contents of box01! 


?> 

 

Get methods don't have to start with the word get and set methods don't have to start with set right?  The two classes above are exactly the same apart from the naming convensions.  I think i'm getting confused with the magic method __get.  That's totally different right?

 

Is it best practice to use the naming convention setWhatever and getWhatever when dealing with objects?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/208204-getter-and-setter-methods/
Share on other sites

I think i'm getting confused with the magic method __get.  That's totally different right?

http://us2.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members

 

I have a quick question on getters and setters in the below classes.  The classes below are exactly the same, return the same values, but named different methods?

 

Right, but what's the point of disrupting the naming conventions?  It just makes everything confusing especially if other people are working with your code.  Also, your method name "outputContents()" isn't an accurate name.  You're returning (getting) $contents, not outputting it...

This link as below talks about property procedure let,get,set and it's rule.

      http://editorial.co.in/software/vbscript_property_let_get_set.php

 

      In PHP such let,get,set procedures may not be available. Nonetheless it works as you have mentioned get,set methods to do similar tasks.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.