i recently made a cookie class but unfortunatly it wasn’t really OOP because i made the class do 2 different things. that is set/get the cookie and also store te cookie. and OOP means every class has its own operation. so i decided to rewrite my class but i am not able to let it work. here is how i tought i should do it.
i made a interface for the CRUD and a Class that implements this interface called CookieStorage. also i made a Cookie class that sets the cookie values etc. but now by creating this two classes it doesn’t work because i get this error: Fatal error: Call to undefined method CookieStorage::getName() in /Applications/MAMP/htdocs/library/lib/CookieStorage.php on line 27
below you can find my code. thanks in advance!
<?php
interface StorageInterface {
public function set(Cookie $cookie);
public function get(Cookie $cookie);
public function update(Cookie $cookie);
public function delete(Cookie $cookie);
}
class CookieStorage implements StorageInterface {
/**
* constructor
*/
public function __construct() {
}
/**
* Create cookie.
*/
public function set(Cookie $cookie) {
return setcookie(
$this->getName(),
$this->getValue(),
$this->getTime(),
$this->getPath(),
$this->getDomain(),
$this->getSecure(), true
);
}
/**
* Get cookie.
*/
public function get(Cookie $cookie) {
return $_COOKIE[$this->getName()];
}
/**
* Update cookie.
*/
public function update(Cookie $cookie) {
return $this->update();
}
public function delete(Cookie $cookie) {
return $this->delete();
}
}
?>
Looks like you want to use
$cookie->getName().$thispoints to the storage class, yet you want to store the properties of the cookie parameter instead.Update:
Your call as mentioned in the comment is correct. If you pass your Cookie object, then in the
setfunction of youCookieStorageyou’ll need to use$cookie, not$this.