Which of these is the correct/better way of writing a class?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Which of these is the correct/better way of writing a class?

Post by JayBird »

Example 1

Code: Select all

class newClass
{
	function newClass()
	{
		$this->doFirstThing();
		$this->doSecondThing();
		$this->doThirdThing();
	}
	
	function doFirstFirstThing()
	{
		// do something here
	}
	
	function doSecondThing()
	{
		// do something here
	}
	
	function doThirdThing()
	{
		// do something here
	}
}
Example 2

Code: Select all

class newClass
{
	function newClass()
	{
		$this->doFirstThing();
	}
	
	function doFirstFirstThing()
	{
		// do something here
		$this->doSecondThing();
	}
	
	function doSecondThing()
	{
		// do something here
		$this->doThirdThing();
	}
	
	function doThirdThing()
	{
		// do something here
	}
}
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

The first way. Chaining method calls like that just makes things less flexible when you want to refactor a bit. Obvious if the same sequence of methods will "always" be run in the same order then you'd set something up but I wouldn't chain them like that, I'd wrapper them into a new method "doAllThings()" :)
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

Sounds sensible :)
Post Reply