Should I avoid using a parameter in a method and use it as much as possible in OOP?

Here is the script

class page { public $name; public $title; public function showhead() { return "<head><title>".$this->title."</title></head>"; } } $mypage = new page; $mypage->title = "My Page title"; $mypage->showhead(); 

and another scenario

 class page { public $name; public function showhead($title) { return "<head><title>".$title."</title></head>"; } } $mypage = new page; $mypage->showhead("My Page title"); 

Among these methods, which are better and what should be avoided? And why?

+6
oop php
source share
6 answers

I think it depends on whether you need this title again. if you do, then create a property to store and retrieve. if you need only once, then use the method parameter.

+8
source share

There is always some tension between passing parameters around (individually or in immutable types of aggregates that actually do not exist in PHP) and storing them somewhere (be it class properties, global variables, etc.). One of the advantages of OOP is that you can save state in objects and benefit from encapsulation (prevents a lot of accidental data overwriting), while avoiding pollution of symbol tables with variables. Using these mutable objects has its own problems, especially if we move on to multi-threaded programming, but this is less true for PHP.

In your particular case, I think it would be better to keep the title in the object. As Siliko said, it seems to refer to page , and besides, you can do things like:

 $page = new page; $page->setTitle("whatever"); ... function doStuff($page) { ... $page->showhead(); } 

And then you do not need to pass $page along with the header.

+5
source share

IMO: because → showhead () has less semantic relation to $ title than the object itself, you should get $ title either as a property, or better through the constructor of the object:

 class page { function __construct($title="") { $this->title = $title } 

So, you can either new page("My page") or assign it later $page->title=... , depending on when you have it.

+3
source share

You must ask yourself: does the title belong to the page? Or this is what the page uses.
This is a basic heuristic that I use to figure out how an object should be constructed in OOP:

  • A combination is a has-a relationship. Does the page have a title? The answer to this question is yes.
  • Inheritance is an is-a relationship. Is the page a title? The answer to this question is no. Note that you prefer composition over inheritance whenever possible. You can usually use any of them.
  • Passing a parameter to a function is a uses-a relation. Does the page use a title? The answer to this question is yes. However, has-a is a stronger relation, then uses-a . Again, sometimes you can use both options, but you may prefer has-a to use-a if possible.
+3
source share

Without additional information, I would say that the first one is "better" because it seems to me that $name and $title are properties that belong to page , so it should be a member of the class. However, if you think the second version is more applicable to your situation, then be sure to use it.

+1
source share

On the side of the note for best practices, it would be nice not to access the object directly, but instead to make it secure and set it / get through public methods, something like this

 class foo { protected $title; public function setTitle($title) { $this->title = $title; } public function getTitle() { return $this->title; } } 

This eliminates the need for any other code working with this data that needs to know how it is actually stored, and instead provides it with an interface, thus encapsulating (isolating) the data. http://en.wikipedia.org/wiki/Information_hiding

+1
source share

All Articles