php 组合模式

<?php

interface RenderableInterface {
	public function render(): string;
}

class InputElement implements RenderableInterface {
	public function render(): string {
		return '<input type="text" />';
	}
}

class TextElement implements RenderableInterface {
	private $text;
	public function __construct($text) {
		$this->text = $text;
	}
	public function render(): string {
		return $this->text;
	}
}

class Form implements RenderableInterface {
	private $elements;

	public function render(): string{
		$formCode = '<form>';

		foreach ($this->elements as $element) {
			$formCode .= $element->render();
		}

		$formCode .= '</form>';
		return $formCode;
	}

	public function addElement(RenderableInterface $element) {
		$this->elements[] = $element;
		return $this;
	}
}

$form = new Form;
$usernameText = new TextElement('用户名');
$usernameInput = new InputElement;
$passwordText = new TextElement('密码');
$passwordInput = new InputElement;
$form->addElement($usernameText)
	->addElement($usernameInput)
	->addElement($passwordText)
	->addElement($passwordInput);

echo $form->render();

php 解释器模式

<?php

interface InterpreterInterface {
	public function interpret(Expression $expression);
}

class Expression {
	public $language;
	public $content;
	public function __construct($language, $content) {
		$this->language = $language;
		$this->content = $content;
	}
}

class ChineseInterpreter implements InterpreterInterface {
	public function interpret($content) {
		for ($i = 0; $i < count($content); $i++) {
			switch ($content[$i]) {
			case '0':
				echo '没有人';
				break;
			case '1':
				echo '一个人';
				break;
			case '2':
				echo '二个人';
				break;
			case '3':
				echo '三个人';
				break;
			case '4':
				echo '四个人';
				break;
			case '5':
				echo '五个人';
				break;
			case '6':
				echo '六个人';
				break;
			case '7':
				echo '七个人';
				break;
			case '8':
				echo '八个人';
				break;
			case '9':
				echo '九个人';
				break;
			default:
				echo '其他';
			}
			echo PHP_EOL;
		}
	}
}

class EnglishInterpreter implements InterpreterInterface {
	public function interpret($content) {
		for ($i = 0; $i < count($content); $i++) {
			switch ($content[$i]) {
			case '0':
				echo 'nobody';
				break;
			case '1':
				echo 'one person';
				break;
			case '2':
				echo 'two people';
				break;
			case '3':
				echo 'three people';
				break;
			case '4':
				echo 'four people';
				break;
			case '5':
				echo 'five people';
				break;
			case '6':
				echo 'six people';
				break;
			case '7':
				echo 'seven people';
				break;
			case '8':
				echo 'eight people';
				break;
			case '9':
				echo 'nine people';
				break;
			default:
				echo 'other';
			}
			echo PHP_EOL;
		}
	}
}

class Interpreter {
	private $interpreter;
	public function execute(Expression $expression) {
		if ($expression->language === 'Chinese') {
			$this->interpreter = new ChineseInterpreter();
		} else {
			$this->interpreter = new EnglishInterpreter();
		}
		$this->interpreter->interpret($expression->content);
	}
}

$expression = new Expression('English', [1, 2, 3, 4, 5]);
$interpreter = new Interpreter();
$interpreter->execute($expression);

php 命令模式

<?php

interface Command {}

class CommandTurnOn implements Command {}
class CommandTurnOff implements Command {}
class CommandSwitchChannel implements Command {}

class TV {
	public function turnOn() {
		echo "TV turn on\n";
	}

	public function turnOff() {
		echo "TV turn off\n";
	}

	public function switchChannel() {
		echo "TV switch channel\n";
	}

	public function execute($command) {
		$methodName = lcfirst(str_replace('Command', '', $command));
		if (method_exists($this, $methodName)) {
			call_user_func([$this, $methodName]);
		}
	}
}

class RemoteController {
	private $tv;
	public function __construct() {
		$this->tv = new TV;
	}
	public function send($str) {
		$this->tv->execute($str);
	}
}

$rc = new RemoteController();
$rc->send(CommandTurnOn::class);
$rc->send(CommandSwitchChannel::class);
$rc->send(CommandTurnOff::class);

php 多例模式

<?php

final class Multiton {
	const INSTANCE_1 = '1';
	const INSTANCE_2 = '2';

	private static $instances = [];

	private function __construct() {}

	public static function getInstance(string $instanceName) {
		if (!isset(self::$instances[$instanceName])) {
			self::$instances[$instanceName] = new self();
		}
		return self::$instances[$instanceName];
	}

	private function __clone() {}
	private function __wakeup() {}
}

php 中介模式

<?php

abstract class InternationalOrganization {
	protected $countries = [];

	// 加入联合国
	public function join(Country $country) {
		if (!in_array($country, $this->countries)) {
			$this->countries[] = $country;
		}
	}

	public function publish(string $message, Country $country) {
		$country->messageArrived($message, $country);
	}

	public function broadcast(string $message, Country $country) {
		foreach ($this->countries as $c) {
			if ($country === $c) {
				continue;
			}
			$c->messageArrived($message, $c);
		}
	}
}

class UnitedNations extends InternationalOrganization {}

abstract class Country {
	private $mediator;

	public function __construct(InternationalOrganization $mediator) {
		$this->mediator = $mediator;
		$this->mediator->join($this);
	}

	public function publish(string $message, Country $country) {
		$this->mediator->publish($message, $country);
	}

	public function broadcast(string $message) {
		$this->mediator->broadcast($message, $this);
	}

	public function messageArrived(string $message, Country $country) {
		echo $message . ' from ' . get_class($country) . PHP_EOL;
	}
}

class USA extends Country {}
class Iraq extends Country {}
class China extends Country {}
class Russia extends Country {}

$un = new UnitedNations;
$usa = new USA($un);
$iraq = new Iraq($un);
$china = new China($un);
$russia = new Russia($un);
$usa->publish('hello, Iraq', $iraq);
$iraq->broadcast('hello, world');

php 快速排序

<?php

$arr = [6, 1, 2, 7, 9, 3, 4, 5, 10, 8];

function quicksort(int $left, int $right) {
	global $arr;
	if ($left > $right) {
		return;
	}
	$temp = $arr[$left];
	$i = $left;
	$j = $right;
	while ($i !== $j) {
		while ($arr[$j] >= $temp && $i < $j) {
			$j--;
		}
		while ($arr[$i] <= $temp && $i < $j) {
			$i++;
		}
		if ($i < $j) {
			$t = $arr[$i];
			$arr[$i] = $arr[$j];
			$arr[$j] = $t;
		}
	}
	$arr[$left] = $arr[$i];
	$arr[$i] = $temp;

	foreach ($arr as $value) {
		echo $value . ' ';
	}
	echo PHP_EOL;

	quicksort($left, $i - 1);
	quicksort($i + 1, $right);
}

quicksort(0, count($arr) - 1);

php 冒泡排序

<?php

class Student {
	public $name;
	public $score;

	public function __construct(string $name, int $score) {
		$this->name = $name;
		$this->score = $score;
	}
}

$stus = [];
$stus[] = new Student('宋 江', 99);
$stus[] = new Student('公孙胜', 44);
$stus[] = new Student('吴 用', 22);
$stus[] = new Student('关 胜', 55);
$stus[] = new Student('卢俊义', 66);
$stus[] = new Student('武大郎', 33);

for ($i = 0, $len = count($stus); $i < $len - 1; $i++) {
	for ($j = 0; $j < $len - $i; $j++) {
		if ($stus[$j]->score < $stus[$j + 1]->score) {
			$t = $stus[$j];
			$stus[$j] = $stus[$j + 1];
			$stus[$j + 1] = $t;
		}
	}
}

print_r($stus);

php 冒泡排序

<?php

/**====================
12, 35, 99, 18, 76
35, 12, 99, 18, 76
35, 99, 12, 18, 76
35, 99, 18, 12, 76
35, 99, 18, 76, 12
====================
35, 99, 18, 76, 12
99, 35, 18, 76, 12
99, 35, 18, 76, 12
99, 35, 76, 18, 12
====================
99, 35, 76, 18, 12
99, 35, 76, 18, 12
99, 76, 35, 18, 12
====================
99, 76, 35, 18, 12
 **/

$arr = [null, 12, 35, 99, 18, 76];
$len = count($arr) - 1;
for ($i = 1; $i <= $len - 1; $i++) {
	for ($j = 1; $j <= $len - $i; $j++) {
		if ($arr[$j] < $arr[$j + 1]) {
			$t = $arr[$j];
			$arr[$j] = $arr[$j + 1];
			$arr[$j + 1] = $t;
		}
	}
}
for ($k = 1; $k <= $len; $k++) {
	echo $arr[$k] . ' ';
}

twitter 点赞效果

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title></title>
	<style>
		.heart {
		  width: 100px;
		  height: 100px;
		  position: absolute;
		  left: 50%;
		  top: 50%;
		  transform: translate(-50%, -50%);
		  background: url(https://www.gekongfei.com/wp-content/uploads/2018/10/web_heart_animation_edge.png) no-repeat;
		  background-position: 0 0;
		  cursor: pointer;
		  animation: fave-heart 1s steps(28);
		}
		.active {
		  background-position: -2800px 0;
		  transition: background 1s steps(28);
		}
		@keyframes fave-heart {
		  0% {
		    background-position: 0 0;
		  }
		  100% {
		    background-position: -2800px 0;
		  }
		}

	</style>
</head>
<body>
	<div class="heart"></div>
	<script>
		let heart = document.querySelector('.heart')
		heart.addEventListener('click', function() {
			heart.classList.toggle('active')
		})
	</script>
</body>
</html>

php 简单桶排序

<?php

$unsortedArr = [3, 1, 19, 12, 9, 2, 7];
$tempArr = array_fill(0, max($unsortedArr) + 1, 0);

foreach ($unsortedArr as $value) {
	$tempArr[$value]++;
}
foreach ($tempArr as $index => $temp) {
	for ($i = 0; $i < $temp; $i++) {
		echo $index . ' ';
	}
}
// output: 1 2 3 7 9 12 19