jcadima
7/31/2017 - 4:45 PM

Interfaces

Interfaces

<?php
/*
============ INTERFACES ============
- interfaces can only have public methods, unlike abstract classes
- can not have variables
- can have Constants
*/

// General Form
interface interafaceName {
	// abstract methods
}

class Child implements interfaceName {
	// Defines the interface methods
	// and may have its own methods
}


// Example
interface Car {
	public function setModel( $name ) ;
	public function getModel() ;
}

class miniCar implements Car {
	private $model ;
	
	public function setModel( $name ) {
		$this->model = $name ;
	}
	
	public function getModel() {
		return $this->model;
	}
	
}

// implementing more than one interface
interface Car {
	public function setModel( $name ) ;
	public function getModel() ;
}

interface Vehicle {
	public function setHasWheels($bool) ;
	public function getHasWheels() ;
}

class miniCar implements Car, Vehicle {
	private $model;
	private $hasWheels;
	
	public function setModel($model) {
		$this->model = $model ;
	}
	
	public function getModel() {
		return $this->model ;
	}
	
	public function setHasWheels($bool) {
		$this->hasWheels = $bool ;
	}
	
	public function getHasWheels() {
		return ($this->hasWheels) ? "has wheels" : "no wheels"  ;
	}
	
}

/*  **********  EXERCISE  ********************
- Create a User class with a protected $username property and methods that can set and
get the $username.
- Create an Author interface with the following abstract methods that can give the user an array of authorship privileges. The first method is setAuthorPrivileges(), and it gets a parameter of $array, and the second method is getAuthorPrivileges().
- Create an Editor interface with methods to set and get the editor’s privileges.
- Now, create an AuthorEditor class that extends the User class, and implements both the Author and the Editor interfaces.
- Create in the AuthorEditor class the methods that it should implement, and the
properties that these methods force us to add to the class.
- Create an object with the name of $user1 from the class AuthorEditor, and set its username to “Balthazar”.
- Set in the $user1 object an array of authorship privileges, with the following privileges: "write text”=", “add punctuation”.
- Set in the $user1 object an array with the following editorial privileges: “edit text”, “edit punctuation”.
*/
interface Author {
	public function setAuthorPriviledges( array $array) ;
	public function getAuthorPriviledges() ;
}

interface Editor {
	public function setEditorPriviledges( array $priv) ;
	public function getEditorPriviledges() ;
}

class User {
	protected $username;
	
	public function setUsername ($uname) {
		$this->username = $uname ;
	}
	
	public function getUsername() {
		return $this->username;
	}
}

class AuthorEditor extends User  implements Author, Editor {

	private $authorPriviledgesArray = [] ;
	private $editorPriviledgesArray = [] ;
	
	public function setAuthorPriviledges( array $arr ) {
		$this->authorPriviledgesArray = $arr ;
	}
	
	public function getAuthorPriviledges() {
		return $this->authorPriviledgesArray ;
	}
	// ---------------------------------------------------
	public function setEditorPriviledges(array $priv) {
		$this->editorPriviledgesArray = $priv ;
	}
	
	public function getEditorPriviledges() {
		return $this->editorPriviledgesArray; 
	}
}


$user1  = new AuthorEditor() ;
$user1->setUsername('Balthazar');
$user1->setAuthorPriviledges(['write text', 'add punctuation']) ;
$user1->setEditorPriviledges (['edit text', 'edit punctuation'] ) ;

// get user1 name and priviledges
$userName = $user1->getUsername() ;
// merge the 2 arrays priviledges
$userPriviledges = array_merge( $user1->getAuthorPriviledges() , $user1->getEditorPriviledges() ) ;

echo $userName . ' has the following priviledges: ' ;
foreach ( $userPriviledges as $privilege ) {
	echo "{$privilege}, " ;
}
echo '.' ;