Re: php5 oop question

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



# lists@xxxxxxxxx / 2007-04-11 23:36:56 -0700:
> Paul Scott wrote:
> >On Wed, 2007-04-11 at 23:22 -0700, Jim Lucas wrote:
> >>Has anybody else seen this style of syntax?
>
> >I don't think that its really useful for anything, except maybe creating
> >overly complex SQL queries.
>
> What about using it for math?

> class Math {
> 	function __construct() {
> 	}
> 	function in($num=null) {
> 		if ( is_null($num) ) {
> 			echo "A number was not given on initialization.";
> 			exit;
> 		}
> 		$this->value = $num;
> 		return $this;
> 	}
> 	function out() {
> 		echo $this->value;
> 	}
> 	function get() {
> 		return $this->value;
> 	}
> 	function add($n) {
> 		$this->value += $n;
> 		return $this;
> 	}
> 	function subtract($n) {
> 		$this->value -= $n;
> 		return $this;
> 	}
> 	function multiply($n) {
> 		$this->value *= $n;
> 		return $this;
> 	}
> 	function divide($n) {
> 		$this->value /= $n;
> 		return $this;
> 	}
> }
> 
> $mObj = new Math();
> $mObj->in(10)->add(90)->divide(5.3)->multiply(10)->out();

The class really represents a number, not "Math", that's too abstract
(pun intended) for an instantiable class.

Besides, you're mixing together two responsibilities, Math should really
be called Number, it should be immutable, and you'll spare someone from
a terrible debugging session caused by someone doing

$math = new math;
$ten = $math->in(10);
$five = $ten->subtract(5);
$fifty = $ten->add(40);

($fifty is actually 45, because $ten is 5 after the subtract() call)

-- 
How many Vietnam vets does it take to screw in a light bulb?
You don't know, man.  You don't KNOW.
Cause you weren't THERE.             http://bash.org/?255991

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux