Skip to content

Instantly share code, notes, and snippets.

@deframe
Last active January 3, 2016 10:49
Show Gist options
  • Save deframe/8451935 to your computer and use it in GitHub Desktop.
Save deframe/8451935 to your computer and use it in GitHub Desktop.
LSP violation example in PHP / Laravel
<?php
interface MenuItemInterface {
public function setTitle($title);
public function getTitle();
public function setUrl($url);
public function getUrl();
}
class RouteBasedMenuItem implements MenuItemInterface {
protected $title;
protected $routeName;
public function __construct($title, $routeName) {
$this->title = $title;
$this->routeName = $routeName;
}
public function setTitle($title) {
$this->title = $title;
}
public function getTitle() {
return $this->title;
}
public function setUrl($url) {
throw new \BadMethodCallException('
The URL of this menu item cannot be set directly; Use setRouteName() instead!
');
}
public function getUrl() {
return \Url::route($this->routeName);
}
public function setRouteName($routeName) {
$this->routeName = $routeName;
}
public function getRouteName() {
return $this->routeName;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment