Last active
January 3, 2016 10:49
-
-
Save deframe/8451935 to your computer and use it in GitHub Desktop.
LSP violation example in PHP / Laravel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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