Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions OOP_exercices/oop_exercices1.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// Create a class called Device.
// The Device has to define the common properties for a device, like, for example,
// a method called getSerialNumber() which returns the Device serial number.

class Device
{
function __construct(int $serialNumber, string $mfr, $firmware)
{
$this->serialNumber = $serialNumber;
$this->mfr = $mfr;
$this->firmware = $firmware;
}

function getSerialNumber()
{
return $this->serialNumber;
}
}

// Then create another one called Mobile and then another one called Tablet.
// The Mobile and Tablet classes have to extend the Device class and define some particular
// properties by their own.

class Mobile extends Device
{
function __construct(string $system, int $mobileGeneration)
{
$this->system = $system;
$this->mobileGeneration = $mobileGeneration;
}

function getGeneration()
{
$g = $this->mobileGeneration;

if ($g === 5) {
echo 'Congrats you have 5G! You can go up to 10Gb per second!!!';
} elseif ($g === 4) {
echo 'Not bad, with 4G your mobile can go between 100 Mbps and 1Gbps!';
} else {
echo 'Please get a new device';
}
}
}

class Tablet extends Device
{
function __construct(string $system, bool $connCapacity)
{
$this->system = $system;
$this->mobileCapacity = $connCapacity;
}

function recogniceSystem()
{
if (strtolower($this->system) == 'android') {
echo 'System detected: you use Android. Good choise!';
} elseif (strtolower($this->system) == 'apple' || strtolower($this->system) == 'ios') {
echo "System detected: you use iOS... no comments";
} else {
echo "Are you using Windows phone?! What's your problem?!";
}
}
}

$nokia = new Device(1324654, 'Nokia', '13A');
$xiaomi = new Mobile('Android', 3);
$mitsubishi = new Tablet('Homebrew', 0);

echo $nokia->getSerialNumber();
echo '</br>';
echo $xiaomi->getGeneration();
echo '</br>';
echo $mitsubishi->recogniceSystem();

// You should also create a class called DeviceManager. This class has to have a method
// called getDeviceSerialNumber(Device $device) which has to call the getSerialNumber() method of Device objects.
// The DeviceManager Instance calls the getDeviceSerialNumber method with each one of the devices objects.

class DeviceManager extends Device
{
function __construct(int $serialNumber, string $mfr, $firmware)
{
parent::__construct($serialNumber, $mfr, $firmware);
$this->serial = $serialNumber;
$this->mfr = $mfr;
$this->firmware = $firmware;
$this->getSerialNumber();
}



function getDeviceSerialNumber(Device $device)
{
$this->getSerialNumber($device);
}
}

$motorola = new DeviceManager(1321561, 'Motorola', '1327B');
echo '</br>';
echo $motorola->getSerialNumber();
echo '</br>';
40 changes: 40 additions & 0 deletions OOP_exercices/oop_exercices2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Create an abstract class Device which defines an abstract method called getDetail($deviceId).
// Create these two methods getId which returns the name of this device and getSerialNo returns
// the serial number for this device.

abstract class Device
{
function __construct(string $name, int $serial)
{
$this->name = $name;
$this->serial = $serial;
}

function getId($name)
{
return $name;
}

function getSerialNo($serial)
{
return $serial;
}

abstract function getDetail($deviceId): string;
}

// Now create a class which extends the Device and implements getDetail($deviceId) method.
// Each device defines in its own way the details. That’s why it is abstracted to let the subclasses implement this method.
// The other methods are common between devices.

class Mobile extends Device
{

function getDetail($deviceId): string
{
return "Mobile brand: $this->name</br>Mobile model: $deviceId";
}
}

$xiaomi = new Mobile('Xiaomi', 4564651);
echo $xiaomi->getDetail('RedMi Note');
78 changes: 78 additions & 0 deletions OOP_exercices/oop_exercices3.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

// Now let’s create an interface for a Device repository.
// First create a Device class with some properties, as you like.

class Device
{
function __construct(string $brand, int $serial, string $system)
{
$this->brand = $brand;
$this->serial = $serial;
$this->system = $system;
}
}

// Then create an interface called DeviceRepository, which defines two methods:
// - create(Device $device) which has an injection of the Device class you have created
// - findById($deviceId) which with the $deviceId returns the Device object but remember this is an interface do not implement methods,
// it just defines them.

interface DeviceRepository
{
public function create(Device $device);
public function findById($deviceId);
}

// Now we need an implementation of the interface DeviceRepository so we are going to create a class called MemoryRepository
// which implements the DeviceRepository. Here you can define the methods as you like.

class MemoryRepository implements DeviceRepository
{
function __construct()
{
$this->deviceArr = [];
}

public function create(Device $device)
{
array_push($this->deviceArr, $device);
return $this->deviceArr;
}

public function findById($deviceId)
{
foreach ($this->deviceArr as $dev) {
if ($dev->serial == $deviceId) {
return $dev;
}
}
}
}

$motorola = new Device("Motorola", 134257, "android");
$x6 = new Device("iPhone", 981321, "ios");
$nokia = new Device("Nokia", 7461, "windows");

$test = new MemoryRepository();
$test->create($motorola);
$test->create($x6);
print_r($test->create($nokia));
echo "</br>";
print_r($test->findById(7461));

// Finally we need to create a class called DeviceManager, which defines a method addDevice(DeviceRepository $repository, Device $device).
// Create an instance of it and inject to this method the implementation you have done.
// Remember this is to check your understanding about OOP.

class DeviceManager
{
function addDevice(DeviceRepository $repository, Device $device)
{
echo "Done";
}
}

$finalTest = new DeviceManager;
echo "</br>";
$finalTest->addDevice($test, $nokia);
87 changes: 87 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ In this project you will learn the basics of OOP using mobile devices as a refer
- [Tools](#tools)
- [OOP Introduction](#oop-introduction)
- [Project files](#project-files)
- [OOP questions](#oop-questions)
- [* What is object-oriented programming in general terms?](#-what-is-object-oriented-programming-in-general-terms)
- [* What is a class?](#-what-is-a-class)
- [* What is an object?](#-what-is-an-object)
- [* What is an instance?](#-what-is-an-instance)
- [* What is a property?](#-what-is-a-property)
- [* What is a method?](#-what-is-a-method)
- [* What is the difference between a function and a method?](#-what-is-the-difference-between-a-function-and-a-method)
- [* What is a constructor?](#-what-is-a-constructor)
- [* What is the difference between a class, an object and an instance?](#-what-is-the-difference-between-a-class-an-object-and-an-instance)
- [* What do we understand about the concept of encapsulation?](#-what-do-we-understand-about-the-concept-of-encapsulation)
- [* What do we understand about the concept of abstraction?](#-what-do-we-understand-about-the-concept-of-abstraction)
- [* What do we understand about the concept of inheritance?](#-what-do-we-understand-about-the-concept-of-inheritance)
- [* What do we understand about the concept of polymorphism?](#-what-do-we-understand-about-the-concept-of-polymorphism)
- [* What do we understand about the concept of Overload?](#-what-do-we-understand-about-the-concept-of-overload)
- [* What do we understand about the concept of Override?](#-what-do-we-understand-about-the-concept-of-override)
- [* What differences exist between the concept of Overload and Override?](#-what-differences-exist-between-the-concept-of-overload-and-override)
- [* What is a static class?](#-what-is-a-static-class)
- [* Look for 3 advantages over object-oriented programming compared to other programming paradigms.](#-look-for-3-advantages-over-object-oriented-programming-compared-to-other-programming-paradigms)
- [* Look for disadvantages of this paradigm.](#-look-for-disadvantages-of-this-paradigm)

## Getting Started

Expand Down Expand Up @@ -134,3 +154,70 @@ Namespaces are qualifiers that solve two different problems:
2. They allow the same name to be used for more than one class

In this file you will learn how to create and use namespaces.

## OOP questions

### * What is object-oriented programming in general terms?
La Programación orientada a objetos es una estructura o paradigma de programación que se basa en la creación de “objetos”, entidades con un estado específico, una identidad y un comportamiento o método.

### * What is a class?
Una clase es un elemento que establece la estructura general de un tipo de objeto y establece sus métodos y propiedades predeterminadas.

### * What is an object?
Como ya hemos dicho, un objeto es una entidad o elemento con propiedades, estados y métodos (funciones) propias.

### * What is an instance?
Los objetos son instancias de las clases, es decir, una concreción de esa estructura general.

### * What is a property?
Son los valores que las clases y los objetos tienen, ya sean heredados u obtenidos al crear el objeto. En resumen, son las variables que contienen los objetos desde el inicio.

### * What is a method?
Los métodos son las funciones que los objetos heredan u obtienen al crearse, tal y como pasa con las propiedades.

### * What is the difference between a function and a method?
La única diferencia entre ambas es su naturaleza, ya que el método es una función asociada al objeto u clase desde el inicio.

### * What is a constructor?
El constructor es la función que permite crear un objeto a partir de una clase, estableciendo sus propiedades y métodos.

### * What is the difference between a class, an object and an instance?
El objeto como elemento general de programación es simplemente un conjunto de datos diferentes y sus funcionalidades.
Una clase es un elemento que establece la estructura, propiedades y funciones por defecto de los objetos que se podrán crear a partir de la misma.
La instancia es la concreción de una clase en un objeto.

### * What do we understand about the concept of encapsulation?
Es la característica de los objetos y de la OOP de recoger un conjunto de datos en un mismo nivel y, juntamente con la característica de ocultación, impide acceder a esos datos fuera del objeto.

### * What do we understand about the concept of abstraction?
Es la característica de los objetos y de la OOP que permite abstraer o “simplificar” funciones y comportamientos complejos en simples métodos. Es decir, permite crear funcionamientos complejos y ocultarlos al usuario.

### * What do we understand about the concept of inheritance?
Es la característica de los objetos y de la OOP que permite transmitir información (propiedades y métodos) y estructuras de un elemento a otro, generalmente entre clases o de clases a objetos.

### * What do we understand about the concept of polymorphism?
Es la característica de los objetos y de la OOP para cambiar y adaptar su forma según los elementos contextuales y los elementos que forman parte del mismo.
En otras palabras, se pueden adaptar los objetos para que tengan diferentes métodos y resultados aún viniendo de la misma clase.

### * What do we understand about the concept of Overload?
Es la capacidad de una clase u objeto de tener diferentes métodos con el mismo nombre que necesitan parámetros diferentes y, por lo tanto, responden a los mismos de formas distintas.

### * What do we understand about the concept of Override?
Es la capacidad de una clase u objeto de redefinir un método de la parent class.

### * What differences exist between the concept of Overload and Override?
Overload es la adaptación de un método a diferentes parámetros, por lo que los argumentos que le pasemos siempre deberán ser diferentes.
En cambio override obliga a mantener la misma estructura para el método modificado, siendo los parámetros a pasar siempre los mismos.

### * What is a static class?
Las clases estáticas son clases que no pueden ser instanciadas ni heredadas, pero si son accesibles (también sus propiedades y métodos) desde fuera de la misma.

### * Look for 3 advantages over object-oriented programming compared to other programming paradigms.
- Versatilidad en su uso y mantenimiento en programación gracias a su reusabilidad.
- Su capacidad de protección y privacidad gracias a sus propiedades de encapsulamiento, visibilidad y abstracción.
- Efectividad al poder usar y adaptar las clases para diferentes usos gracias a la herencia.

### * Look for disadvantages of this paradigm.
- Tamaño, ya que tanto las clases como sus instancias ocupan espacios de memoria.
- Performance, a más tamaño menos velocidad en su ejecución (aunque no siempre es así).
- Complejidad, que tanto le permite construir elementos más complejos y facilitar su uso al usuario, como complicar su creación.