Windowing System#
Welcome to Windowing System on Exercism's PHP Track.
If you need help running the tests or submitting your code, check out HELP.md.
If you get stuck on the exercise, check out HINTS.md, but try and solve it without using those first :)
Introduction#
Classes#
Classes are a unit of code organization in PHP.
Code represents the behavior of an item or the behavior of a process can be grouped together as a class.
Functions that are assigned to a class are called methods by convention.
<?php
class Door
{
function lock()
{
// ...
}
function unlock()
{
// ...
}
}
Classes are instantiated with the new keyword, this allocates memory for the class instance and calls the classes constructor method.
Instantiated classes may be assigned to a variable.
To invoke the methods of a class instance, we can use the -> access operator.
<?php
$a_door = new Door();
$a_door->lock();
$a_door->unlock();
An instantiated class may reference its own instance using the special $this variable.
Classes may also have properties, these act as variables that are tied to the instance of the class object.
These properties may be referenced internally or externally.
<?php
class Car
{
public $color;
function __construct($color)
{
$this->color = $color;
}
function getColor()
{
return $this->color;
}
}
$a_car = new Car("red");
$a_car->color; // => "red" by accessing the property
$a_car->getColor(); // => "red" by invoking the instance method
Instructions#
In this exercise, you will be simulating a windowing based computer system. You will create some windows that can be moved and resized. The following image is representative of the values you will be working with below.
╔════════════════════════════════════════════════════════════╗
║ ║
║ position::$y,_ ║
║ position::$x \ ║
║ \<---- size::$width ----> ║
║ ^ *──────────────────────┐ ║
║ | │ title │ ║
║ | ├──────────────────────┤ ║
║ | │ │ ║
║ size::$height │ │ ║
║ | │ contents │ ║
║ | │ │ ║
║ | │ │ ║
║ v └──────────────────────┘ ║
║ ║
║ ║
╚════════════════════════════════════════════════════════════╝
1. Define the properties of the Program Window#
Using the provided class scaffold, provide the properties for the y, x, height, and width values.
<?php
$window = new ProgramWindow();
$window->y; // => null
$window->x; // => null
$window->height; // => null
$window->width; // => null
2. Define the initial values for the program window#
Define a constructor function for ProgramWindow.
It should not take any arguments, but during the constructor execution set the default values for its properties.
It should set the initial y and x values to 0.
It should set the initial height and width to a 600x800 screen size.
<?php
$window = new ProgramWindow();
$window->y; // => 0
$window->x; // => 0
$window->height; // => 600
$window->width; // => 800
3. Define a function to resize the window#
Define a new class in Size.php for a Size class.
It should take constructor arguments to set the height and width properties.
Additionally ProgramWindow requires a resize function, which receives the Size object instance and updates its own properties.
<?php
$window = new ProgramWindow();
$size = new Size(764, 1080);
$window->resize($size)
$window->height;
// => 764
$window->width;
// => 1080
4. Define a function to move the window#
Define a new class in Position.php for a Position class.
It should take constructor arguments to set the y and x properties.
Additionally ProgramWindow requires a move function, which receives the Position object instance and updates its own properties.
<?php
$window = new ProgramWindow();
$position = new Position(80, 313);
$window->move($position)
$window->y;
// => 80
$window->x;
// => 313
Source#
Created by#
- @neenjaw