danieliser
11/6/2015 - 7:45 AM

A short demo of how you can chain set and get methods. This could be the basis for a plain English PHP library where you functions are very

A short demo of how you can chain set and get methods. This could be the basis for a plain English PHP library where you functions are very semantic and programming is more like writing sentences than code.

<?php
class Chain_Demo {

	private $variable = null;

	private $method = 'get';

	public function set() {
		$this->method = 'set';
		return $this;
	}
	public function get() {
		$this->method = 'get';
		return $this;
	}

	public function variable( $value = null ) {
		switch($this->method) {
			case 'set':
				$this->variable = $value;
				return $this;
				break;

			case 'get':
				return $this->variable;
				break;
		}
		return null;
	}

}

$chaining = new Chain_Demo();
$chaining->set()->variable('value');
echo $chaining->get()->variable(); // value
echo $chaining->set()->variable('value')->get()->variable(); // value