Page 1 of 1

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

Posted: Wed Nov 01, 2006 10:36 am
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
	}
}

Posted: Wed Nov 01, 2006 11:03 am
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()" :)

Posted: Wed Nov 01, 2006 11:10 am
by JayBird
Sounds sensible :)