-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathassociation.php
61 lines (51 loc) · 1.46 KB
/
association.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/**
* This example just to simulate how Association can be work
* 2nees.com
*/
class Website {
private string $content;
/**
* Website constructor.
* @param string $content
*/
public function __construct(string $content)
{
$this->content = $content;
}
public function printContent(WebsiteContent $websiteContent): void {
$websiteContent->setText($this->content);
$websiteContent->print();
}
}
abstract class WebsiteContent {
protected string $text;
public function setText($text): void {
$this->text = $text;
}
abstract function print(): void;
}
class ListContent extends WebsiteContent {
function print(): void
{
echo "<ul><li>{$this->text}</li></ul>" . PHP_EOL;
}
}
class ParagraphContent extends WebsiteContent {
function print(): void
{
echo "<p>{$this->text}</p>" . PHP_EOL;
}
}
// Client
$listContent = new ListContent();
$paragraphContent = new ParagraphContent();
$website = new Website("2nees.com");
$website->printContent($listContent);
$website->printContent($paragraphContent);
unset($website);
// Since its Association relation, other object can complete life cycle normally...
$listContent->setText("List will not effect if we remove website, I'm Association With Website!");
$listContent->print();
$paragraphContent->setText("Paragraph will not effect if we remove website, I'm Association With Website!");
$paragraphContent->print();