# PHP Example class namespace namespaceName; //Example include other file require_once("ParentClassName.php"); ``` /** * Example of class (with Parent class) */ class ClassName extends ParentClassName { private $privateMemberVariable; public $publicMemberVariable; /** * Example of constructor with argument */ public function __construct(string $constructorStringArgument) { $this->privateMemberVariable = "dont forget $this-> on member variables "; } /** * Example of returning a member variable */ public function getMember() : string { //note the type-safe return value of type string return $this->privateMemberVariable; } /** * Example string concatination */ public function getMemberPlusOther(string $other) : string { return $this->privateMemberVariable . $other; //Note the "." operator } } ``` ``` An object is used using the "new" keyword $e = new Exception(); Note that parameters might need to be sent $e = new Exception("Something bad happened"); ``` ``` Get and set <?php class Adress { } class Person { private $adress; /* Method parameter with type-hinting */ public function setAdress(Adress $typedArgument) { $this->adress = $typedArgument; } /* Method to access a private member variable*/ public function getAdress() { return $this->adress; } } ``` ``` //if-statements if ($i < $sum) { $sum = $i; } else if ($i > $sum) { $sum = $i; } else { } //for-loop with variable $arr = array(1,2,3); for ($i = 0; $i < count($arr); $i++) { echo "index $i value " . $arr[$i] . " "; } //foreach-loop foreach ($arr as $key => $value) { echo "index: $key value: $value "; } //while loop while (true) { echo "this will result in a memory overflow..."; } //exception try { throw new \Exception("an exeption occured"); } catch(\Exception $e) { echo "An exception was captured"; } ``` ``` Good to know PHP Api-methods //Check if a variable is a atring if (is_string($name) === false) { } //count the number of values in an array $someArray = array(); if (count($someArray) == 0) { } //check if an unknown variable is a number if (is_numeric($element)) { echo "'{$element}' is numeric", PHP_EOL; } else { echo "'{$element}' is NOT numeric", PHP_EOL; } //Check if a number is finite if (is_finite($largeNumber)) { echo "the number is finite"; } //Check if a number is infinite if (is_infinite($largeNumber)) { echo "the number is infinite"; } ``` ``` Global scope variables A global scope variable can be declared directly in the PHP file. <?php $globalVariable = 4; Function parameters and Local variables Function parameters does not have to have a type even if I recommend that you do. A variable that is locally declared in the scope of the method can only be used inside the method. <?php function exampleFunction($parameter) { $localVariable = $parameter; } Class member variables The following PHP application creates a public member variable named "name" Variable-declarations in PHP does not use a type-hint except for classes and arrays as arguments to methods or functions. <?php //Declaration class Person { public $name = "Daniel"; protected $age = 35; private $phoneNumber = "555-12345"; } //Usage $student = new Person(); //create an object $student->name = "Linda"; echo $student->phoneNumber; //not allowed due to the private scope Visibility The three keywords private, protected and public decides how the variable or method can be used from other classes. private - can only be used within the class protected - can only be used within the class and by classes that inherit from the class public - can be used by anyone ``` ``` Import files To access a class in another file use the "require_once" method. <?php require_once("Person.php"); $p = new Person("Daniel"); ``` ``` Example, store a value in a member variable Note that for class-member variables and functions you always need to access them through "$this->" <?php class Adress { } class Person { private $adress; /* Method parameter with type-hinting */ public function setAdress(Adress $typedArgument) { $this->adress = $typedArgument; } /* Method to access a private member variable*/ public function getAdress() { return $this->adress; } } Dependency Injection A value supplied in a class constructor is either used to compute the inner state of the object (used inside the constructor itself) or it is stored in a member variable for use in member methods. <?php class Adress { } class Person { private $adress; public function __construct(Adress $typedArgument) { $this->adress = $typedArgument; } /* Method to access a private member variable*/ public function getAdress() : Adress{ return $this->adress; } } ```