Skip to content

Commit a194efc

Browse files
author
Joost van Veen
committed
Lesson 16: static properties and methods
1 parent d6b35da commit a194efc

15 files changed

+625
-1
lines changed

16_statics/Acme/App/Administrator.php

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Acme\App;
4+
5+
/**
6+
* This class extends Acme\App\User
7+
*/
8+
class Administrator extends User {
9+
10+
public function __construct() {
11+
$this->isAdmin = true;
12+
}
13+
14+
/**
15+
* Log in a user
16+
* @return string
17+
*/
18+
public function login(){
19+
20+
// Login in our member using the logic in the parent class.
21+
$message = parent::login();
22+
23+
// Add some administrator-spcific logic
24+
return $message . ' ... Log this action in an administrator\'s table';
25+
}
26+
27+
/**
28+
* This method is for Administrators only
29+
*/
30+
public function reportForDuty ()
31+
{
32+
// Do stuff ...
33+
}
34+
35+
}

16_statics/Acme/App/Member.php

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Acme\App;
4+
5+
/**
6+
* This class extends Acme\App\User
7+
*/
8+
class Member extends User {
9+
10+
/**
11+
* Log in a user
12+
* @return string
13+
*/
14+
public function login(){
15+
16+
// Login in our member using the logic in the parent class.
17+
$message = parent::login();
18+
19+
// Add some member-spcific logic
20+
return $message . ' ... Set a flag in the online members table';
21+
}
22+
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Acme\App\Repositories;
4+
5+
/**
6+
* The post repository that fetches posts from a database
7+
*/
8+
class PostDatabaseRepository implements PostRepositoryInterface
9+
{
10+
protected $db;
11+
12+
public function __construct ()
13+
{
14+
// Store database connection
15+
}
16+
17+
/**
18+
* @see \Acme\App\Repositories\PostRepositoryInterface::All()
19+
*/
20+
public function All ()
21+
{
22+
// Fetch all posts from db
23+
}
24+
25+
/**
26+
* @see \Acme\App\Repositories\PostRepositoryInterface::Find()
27+
*/
28+
public function Find ($id)
29+
{
30+
// Fetch a single post from db
31+
}
32+
}
33+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Acme\App\Repositories;
4+
5+
use stdClass;
6+
7+
/**
8+
* The post repository that fetches posts from a json file
9+
*/
10+
class PostJsonRepository implements PostRepositoryInterface {
11+
12+
protected $posts = array();
13+
14+
/**
15+
* Config
16+
*/
17+
public function __construct() {
18+
$posts = json_decode(file_get_contents(dirname(__FILE__) . '/posts.json'), true);
19+
foreach ($posts as $key => $post){
20+
$this->posts[$key] = (object) $post;
21+
}
22+
}
23+
24+
/**
25+
* @see \Acme\App\Repositories\PostRepositoryInterface::All()
26+
*/
27+
public function All ()
28+
{
29+
return $this->posts;
30+
}
31+
32+
/**
33+
* @see \Acme\App\Repositories\PostRepositoryInterface::Find()
34+
*/
35+
public function Find ($id)
36+
{
37+
return isset($this->posts[$id]) ? $this->posts[$id] : new stdClass;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Acme\App\Repositories;
4+
5+
/**
6+
* The contract for all post repositories. They must all use the emthods defined in this interface.
7+
*/
8+
interface PostRepositoryInterface {
9+
10+
/**
11+
* Return all posts, as an array of objects
12+
* $post->title
13+
* $post->body
14+
*
15+
* @return array
16+
*/
17+
public function All();
18+
19+
/**
20+
* Return a single post
21+
* $post->title
22+
* $post->body
23+
*
24+
* @param integer $id
25+
* @return object
26+
*/
27+
public function Find($id);
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Acme\App\Repositories;
4+
5+
use stdClass;
6+
7+
/**
8+
* The post repository that fetches posts from an RSS feed
9+
*/
10+
class PostRssRepository implements PostRepositoryInterface {
11+
12+
protected $posts = array();
13+
14+
public function __construct() {
15+
$xml = simplexml_load_file(dirname(__FILE__) . '/posts.xml');
16+
$posts = $xml->xpath('channel/item');
17+
foreach ($posts as $key => $post){
18+
$post = json_decode(json_encode($post));
19+
$post->body = $post->description;
20+
unset($post->description);
21+
$this->posts[$key + 1] = $post;
22+
}
23+
}
24+
25+
/**
26+
* @see \Acme\App\Repositories\PostRepositoryInterface::All()
27+
*/
28+
public function All ()
29+
{
30+
return $this->posts;
31+
}
32+
33+
/**
34+
* @see \Acme\App\Repositories\PostRepositoryInterface::Find()
35+
*/
36+
public function Find ($id)
37+
{
38+
return isset($this->posts[$id]) ? $this->posts[$id] : new stdClass;
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"1": {
3+
"title":"This is my first post from json",
4+
"body":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ut leo vitae urna eleifend fringilla. Ut tincidunt risus et orci viverra non accumsan urna bibendum. Fusce sagittis tortor eu justo fermentum egestas. Sed interdum, leo a tempus con."
5+
},
6+
"2": {
7+
"title":"This is my second post from json",
8+
"body":"Nullam eros odio, luctus et rutrum sed, varius ac enim. Aliquam nunc ante, lacinia sed porta nec, iaculis a ligula. Phasellus sagittis cursus purus, non interdum elit aliquam non. Nam lorem nibh, facilisis at scelerisque nec, tempus ac lacus maecena."
9+
},
10+
"3": {
11+
"title":"This is my third post from json",
12+
"body":"Nullam eros odio, luctus et rutrum sed, varius ac enim. Aliquam nunc ante, lacinia sed porta nec, iaculis a ligula. Phasellus sagittis cursus purus, non interdum elit aliquam non. Nam lorem nibh, facilisis at scelerisque nec, tempus ac lacus maecena."
13+
}
14+
}
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<rss version="2.0">
3+
<channel>
4+
<item>
5+
<title>This is my first post from xml</title>
6+
<description>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ut leo vitae urna eleifend fringilla. Ut tincidunt risus et orci viverra non accumsan urna bibendum. Fusce sagittis tortor eu justo fermentum egestas. Sed interdum, leo a tempus con.</description>
7+
</item>
8+
<item>
9+
<title>This is my second post from xml</title>
10+
<description>Nullam eros odio, luctus et rutrum sed, varius ac enim. Aliquam nunc ante, lacinia sed porta nec, iaculis a ligula. Phasellus sagittis cursus purus, non interdum elit aliquam non. Nam lorem nibh, facilisis at scelerisque nec, tempus ac lacus maecena.</description>
11+
</item>
12+
<item>
13+
<title>This is my third post from xml</title>
14+
<description>Nullam eros odio, luctus et rutrum sed, varius ac enim. Aliquam nunc ante, lacinia sed porta nec, iaculis a ligula. Phasellus sagittis cursus purus, non interdum elit aliquam non. Nam lorem nibh, facilisis at scelerisque nec, tempus ac lacus maecena.</description>
15+
</item>
16+
</channel>
17+
</rss>

16_statics/Acme/App/User.php

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace Acme\App;
4+
5+
abstract class User {
6+
7+
/**
8+
* @var string
9+
*/
10+
protected $email;
11+
12+
/**
13+
* @var string
14+
*/
15+
protected $password;
16+
17+
/**
18+
* @var boolean
19+
*/
20+
protected $isAdmin = false;
21+
22+
/**
23+
* All properties that can be set directly
24+
* @var array
25+
*/
26+
protected $fillable = array('email', 'password');
27+
28+
/**
29+
* All properties that can be gotten directly
30+
* @var array
31+
*/
32+
protected $accessible = array('email', 'password');
33+
34+
/**
35+
* Class configuration only
36+
* @param array $params
37+
*/
38+
public function __construct(Array $params = array()) {
39+
40+
if (count($params)) {
41+
foreach ($params as $key => $value) {
42+
$this->$key = $value;
43+
}
44+
}
45+
}
46+
47+
/**
48+
* Directly set inaccessible but existing properties, if in $this->fillable array
49+
* @param string $name
50+
* @param mixed $value
51+
* @return void
52+
*/
53+
public function __set ($name, $value) {
54+
55+
// Do not set if not fillable
56+
if (! in_array($name, $this->fillable)) {
57+
return false;
58+
}
59+
60+
if (isset($this->$name)) {
61+
$this->$name = $value;
62+
}
63+
}
64+
65+
/**
66+
* Directly get inaccessible but existing properties, if in $this->accesible array
67+
* @param string $name
68+
* @return mixed
69+
*/
70+
public function __get ($name) {
71+
72+
// Do not return if not accessible
73+
if (! in_array($name, $this->accessible)) {
74+
return NULL;
75+
}
76+
77+
return isset($this->$name) ? $this->$name : NULL;
78+
}
79+
80+
/**
81+
* Return accessible properties as a json object
82+
* @return string
83+
*/
84+
public function __toString () {
85+
86+
$data = array();
87+
88+
// Only add property accessible
89+
foreach ($this->accessible as $key) {
90+
$data[$key] = $this->$key;
91+
}
92+
93+
return json_encode($data);
94+
}
95+
96+
/**
97+
* Log in a user
98+
* @return string
99+
*/
100+
public function login(){
101+
return 'Logging in a user ...';
102+
}
103+
104+
/**
105+
* Log in a user
106+
* @return string
107+
*/
108+
public function logout(){
109+
return 'Logging out ...';
110+
}
111+
112+
}

0 commit comments

Comments
 (0)