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
21 changes: 4 additions & 17 deletions 01-classes.php
Original file line number Diff line number Diff line change
@@ -1,21 +1,8 @@
<?php
//======================================================================
// ASSEMBLER SCHOOL - PHP Object Oriented Programming
//======================================================================

/* File 01 - Classes */
// Define a class in PHP
class User {

// To create classes we use reserved word class.
class Mobile
{
}
}

// We can instantitate classes like this
$modernMobile = new Mobile();
$oldMobile = new Mobile();

// we can use var_dump so we can visualize our new class objects

var_dump($modernMobile);
echo "<br>";
var_dump($oldMobile);
?>
62 changes: 18 additions & 44 deletions 02-properties.php
Original file line number Diff line number Diff line change
@@ -1,50 +1,24 @@
<?php
//======================================================================
// ASSEMBLER SCHOOL - PHP Object Oriented Programming
//======================================================================

/* File 02 - Properties */
class User{
// public, can be accessed from everywhere
public string $name = 'Juan';

// classes have properties that contain information about that object defined by the class
class Mobile {
public $name; // Public makes sure we can access this properties outside the class.
public $chipset;
public $internalMemory;
}
// protected, can be accessed only with the class by inheriting or from parent classes
protected string $lastName = 'Solano';

$modernMobile = new Mobile();
// private, can only be accessed by the class that defines the member
private int $age = 36;
}

// in OOP and PHP we use the arrow operator ( -> ) for accessing properties
$user = new User();
var_dump($user);
echo '<br><br>';
echo $user->name;
echo '<br><br>';
// lastName and Age give errors for protected and private, except with var_dump and Class
echo $user->lastName;
echo '<br><br>';
echo $user->age;

// we can assign those public properties in our class with this syntax
$modernMobile->name = "Samsung s20";
$modernMobile->chipset = "Exynos";
$modernMobile->internalMemory = 128;

echo "<br>";

// we can also access properties value by the arrow operator
echo $modernMobile->chipset;

// what happens if we assign a non existing value or a wrong value?
$modernMobile->ramMemory = 1;
$modernMobile->internMemory = 8;

echo "<pre>";
var_dump($modernMobile);
echo "</pre>";

echo "<br>";

//-----------------------------------------------------
// arrow operator is unique in objects and must not be
// confused with double arrow operator used in arrays
//-----------------------------------------------------

// this is an example using an array and double arrow operators
$mobileArray = [
'name' => 'Xiaomi Mi10',
'chipset' => 'Snapdragon',
'internalMemory' => 64
];
echo $mobileArray['chipset'];
?>
33 changes: 33 additions & 0 deletions 03-Constant-static.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

class User{
// CONST cant be variable, property or function call
const USER_ROLE = 'user';

private string $name = 'Juan';
private string $lastName = 'Solano';
private int $age = 36;

// STATIC must be declared ClassName::$var;
public static string $userPrefix = 'ES-2022_';


}

$user = new User();

echo '<pre>';
var_dump($user);
echo '</pre>';

echo '<br><br>';

// echo $user->name;
// echo '<br><br>';
// echo $user->lastName;
// echo '<br><br>';
echo User::USER_ROLE;
echo '<br><br>';
echo User::$userPrefix;

?>
28 changes: 0 additions & 28 deletions 03-methods.php

This file was deleted.

61 changes: 0 additions & 61 deletions 04-getters.php

This file was deleted.

19 changes: 19 additions & 0 deletions 04.a-Constructors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

class User {
const ONLINE = 'Active';
private string $name = 'Juan';
private string $lastName = 'Solano';
private int $age = 36;

// CONSTRUCTOR -> @param string $name
// $this->name = $name;

public function __construct(string $name, string $lastName, int $age) {
$this->name = $name;
$this->lastName = $lastName;
$this->age = $age;
}
}

?>
24 changes: 24 additions & 0 deletions 04.b-Destructors.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

class User {
const USER_PATH = './prueba/hola.json';
private string $name = 'Juan';
private string $lastName = 'Solano';
private int $age = 36;

// CONSTRUCTOR -> @param string $name
// $this->name = $name;

public function __construct(string $name, string $lastName, int $age) {
$this->name = $name;
$this->lastName = $lastName;
$this->age = $age;
}

public function __destruct() {
unlink(self::USER_PATH . 'hola.json');
}

}

?>
31 changes: 31 additions & 0 deletions 05-Methods.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

class User {
const USER_ROLE = 'user';

private $name = 'Juan';
private $lastName = 'Solano';
private $age = '36';

public function setName($name) {
$this->name = $name;
return $this;
}

public function getName() {
return $this->name;
}

public static function getPermissions() {
return ['read',
'write'
];
}
}

$user = new User();
echo $user->getName();
echo '<br><br>';
print_r(User::getPermissions());

?>
42 changes: 0 additions & 42 deletions 05-setters.php

This file was deleted.

24 changes: 24 additions & 0 deletions 06-Inheritance.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

class User {
const USER_ROLE = 'user';

protected string $name;
protected string $lastName;
protected int $age;
private string $NickName;
}

// Class Admin can access to User properties like name, lastName and age because are protected, but nickName are private and cant access
// and add $resources only available for Admin

class Admin extends User {
const USER_ROLE = 'Admin';
private array $resources = [
'/users/management',
'/users/emails',
'/users/files',
];
}

?>
Loading