My attempts at exercism.org
1# Windowing System
2
3Welcome to Windowing System on Exercism's PHP Track.
4If you need help running the tests or submitting your code, check out `HELP.md`.
5If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
6
7## Introduction
8
9## Classes
10
11Classes are a unit of code organization in PHP.
12Code represents the behavior of an item or the behavior of a process can be grouped together as a class.
13Functions that are assigned to a class are called `methods` by convention.
14
15```php
16<?php
17
18class Door
19{
20 function lock()
21 {
22 // ...
23 }
24
25 function unlock()
26 {
27 // ...
28 }
29}
30```
31
32Classes are instantiated with the `new` keyword, this allocates memory for the class instance and calls the classes constructor method.
33Instantiated classes may be assigned to a variable.
34To invoke the methods of a class instance, we can use the `->` access operator.
35
36```php
37<?php
38
39$a_door = new Door();
40$a_door->lock();
41$a_door->unlock();
42```
43
44An instantiated class may reference its own instance using the special `$this` variable.
45Classes may also have properties, these act as variables that are tied to the instance of the class object.
46These properties may be referenced internally or externally.
47
48```php
49<?php
50
51class Car
52{
53 public $color;
54
55 function __construct($color)
56 {
57 $this->color = $color;
58 }
59
60 function getColor()
61 {
62 return $this->color;
63 }
64}
65
66$a_car = new Car("red");
67$a_car->color; // => "red" by accessing the property
68$a_car->getColor(); // => "red" by invoking the instance method
69```
70
71## Instructions
72
73In this exercise, you will be simulating a windowing based computer system.
74You will create some windows that can be moved and resized.
75The following image is representative of the values you will be working with below.
76
77```text
78 ╔════════════════════════════════════════════════════════════╗
79 ║ ║
80 ║ position::$y,_ ║
81 ║ position::$x \ ║
82 ║ \<---- size::$width ----> ║
83 ║ ^ *──────────────────────┐ ║
84 ║ | │ title │ ║
85 ║ | ├──────────────────────┤ ║
86 ║ | │ │ ║
87 ║ size::$height │ │ ║
88 ║ | │ contents │ ║
89 ║ | │ │ ║
90 ║ | │ │ ║
91 ║ v └──────────────────────┘ ║
92 ║ ║
93 ║ ║
94 ╚════════════════════════════════════════════════════════════╝
95```
96
97## 1. Define the properties of the Program Window
98
99Using the provided class scaffold, provide the properties for the `y`, `x`, `height`, and `width` values.
100
101```php
102<?php
103
104$window = new ProgramWindow();
105$window->y; // => null
106$window->x; // => null
107$window->height; // => null
108$window->width; // => null
109```
110
111## 2. Define the initial values for the program window
112
113Define a constructor function for `ProgramWindow`.
114It should not take any arguments, but during the constructor execution set the default values for its properties.
115It should set the initial `y` and `x` values to `0`.
116It should set the initial `height` and `width` to a `600x800` screen size.
117
118```php
119<?php
120
121$window = new ProgramWindow();
122$window->y; // => 0
123$window->x; // => 0
124$window->height; // => 600
125$window->width; // => 800
126```
127
128## 3. Define a function to resize the window
129
130Define a new class in `Size.php` for a Size class.
131It should take constructor arguments to set the `height` and `width` properties.
132Additionally `ProgramWindow` requires a resize function, which receives the `Size` object instance and updates its own properties.
133
134```php
135<?php
136
137$window = new ProgramWindow();
138$size = new Size(764, 1080);
139$window->resize($size)
140
141$window->height;
142// => 764
143$window->width;
144// => 1080
145```
146
147## 4. Define a function to move the window
148
149Define a new class in `Position.php` for a Position class.
150It should take constructor arguments to set the `y` and `x` properties.
151Additionally `ProgramWindow` requires a move function, which receives the `Position` object instance and updates its own properties.
152
153```php
154<?php
155
156$window = new ProgramWindow();
157$position = new Position(80, 313);
158$window->move($position)
159
160$window->y;
161// => 80
162$window->x;
163// => 313
164```
165
166## Source
167
168### Created by
169
170- @neenjaw