Wstęp do programowania w języku PHP
Programowanie obiektowe PHP OOP
O czym jest ta prezentacja wstęp do programowania obiektowego cechy języka php przestrzenie nazw composer automatyczne ładowanie klas
Programowanie obiektowe klasa enkapsulacja metody magiczne dziedziczenie klasy abstrakcyjne interfejsy cechy przestrzenie nazw
Klasa /** * Class representing any electronic Device * @author John Doe <john.doe@example.com> */ class Device { /** * Outer case color * @var string */ public $color = 'white'; /** * Returns device's unique s/n * @return string */ public function getserial() { return '1M8GDM9AKP042788'; /** * Performs soft restart on a device */ public function restart() {
Klasa class Device { public $color = 'white'; public function getserial () { return '1M8GDM9AKP042788' ; public function restart () { $mydevice = new Device(); $mydevice ->color = 'black';
Enkapsulacja class Device { private $color = 'white'; public function getserial () { return '1M8GDM9AKP042788' ; $mydevice = new Device(); $mydevice ->color = 'black'; Fatal error: Cannot access private property Device::$color
Enkapsulacja class Device { private $color = 'white'; Modyfikatory dostępu private protected public public function getserial () { return '1M8GDM9AKP042788' ; $mydevice = new Device(); $mydevice ->color = 'black'; Fatal error: Cannot access private property Device::$color
Konstruktor class Phone { private $imei; public function construct ($imei) { $this->imei = $imei;
Konstruktor class Phone { private $imei; public function construct ($imei) { $this->imei = $imei;
Konstruktor class Phone { private $imei; public function construct ($imei) { $this->imei = $imei; $foo = new Phone();
Konstruktor class Phone { private $imei; public function construct ($imei) { $this->imei = $imei; $foo = new Phone(); Warning:Missing argument 1 for Phone:: construct ()
Konstruktor class Phone { private $imei; public function construct ($imei) { $this->imei = $imei; $foo = new Phone('75473858437534325' );
Metody Magiczne class Phone { private $attributes = array(); public function construct (array $attributes ) { $this->attributes = $attributes ;
Metody Magiczne class Phone { private $attributes = array(); public function construct (array $attributes ) { $this->attributes = $attributes ; public function get($name) { if(array_key_exists ($name, $this->attributes )) { return $this->attributes [$name]; return null;
Metody Magiczne class Phone { //... public function get($name) { if(array_key_exists ($name, $this->attributes )) { return $this->attributes [$name]; return null; $phone = new Phone([ 'color' => 'black', 'mpix' => 8 ]); echo $phone->mpix;
Metody Magiczne class Phone { //... public function set($name, $value ) { $this->attributes[$name] = $value; $phone = new Phone([ 'color' => 'black', 'mpix' => 8 ]);
Metody Magiczne class Phone { //... public function set($name, $value ) { $this->attributes[$name] = $value; $phone = new Phone([ 'color' => 'black', 'mpix' => 8 ]); $phone->mpix = 16; echo $phone->mpix;
Dziedziczenie class Phone extends Device { public function call($number) { // (...) $myphone = new Phone();
Dziedziczenie class Phone extends Device { public function call($number) { // (...) $myphone = new Phone(); $myphone ->color = 'blue';
Dziedziczenie class Phone extends Device { public function call($number) { // (...) $myphone = new Phone(); $myphone ->color = 'blue'; $serial = $myphone ->getserial ();
Dziedziczenie class Phone extends Device { public function call($number) { // (...) $myphone = new Phone(); $myphone ->color = 'blue'; $serial = $myphone ->getserial (); $myphone ->call('+48 708 477 130' );
Klasa abstrakcyjna abstract class PhoneAbstract { public $color = 'white'; public abstract function call($number); ; $somephone = new PhoneAbstract ();
Klasa abstrakcyjna abstract class PhoneAbstract { public $color = 'white'; public abstract function call($number); ; $somephone = new PhoneAbstract (); Fatal error: Cannot instantiate abstract class Phone
Klasa abstrakcyjna class IPhone extends PhoneAbstract { ; $iphone = new IPhone();
Klasa abstrakcyjna class IPhone extends PhoneAbstract { ; $iphone = new IPhone(); Fatal error: Class IPhone contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Phone::call)
Klasa abstrakcyjna class IPhone extends PhoneAbstract { public function call($number){ //... ;
Klasa abstrakcyjna vs interfejs abstract class Camera { public abstract function record(); class IPhone extends Phone, Camera {
Klasa abstrakcyjna vs interfejs abstract class Camera { public abstract function record(); class IPhone extends Phone, Camera {
Klasa abstrakcyjna vs interfejs abstract class Camera{ public abstract function record(); class IPhone extends Phone, Camera { Brak wielokrotnego dziedziczenia w języku PHP
Interface common/phoneinterface.php interface PhoneInterface { public function call($number); common/camerainterface.php interface CameraInterface { public function record();
Interface common/phoneinterface.php interface PhoneInterface { public function call($number); common/camerainterface.php interface CameraInterface { public function record(); class IPhone implements PhoneInterface, CameraInterface { public function call($number) { /*... */ public function record() { /*... */
Interface common/phoneinterface.php interface PhoneInterface { public function call($number); Tylko metody publiczne common/camerainterface.php interface CameraInterface { public function record(); class IPhone implements PhoneInterface, CameraInterface { public function call($number) { /*... */ public function record() { /*... */
Cechy trait PhoneBehavior { public function call($number) { /*... */
Cechy trait PhoneBehavior { public function call($number) { /*... */ trait CameraBehavior { public function record() { /*... */
Cechy trait PhoneBehavior { public function call($number) { /*... */ trait CameraBehavior { public function record() { /*... */ class IPhone { use PhoneBehavior, CameraBehavior ; $iphone = new IPhone(); $iphone->record();
Przestrzenie nazw smartphone/camerainterface.php /** * This is a smartphone camera interface, every smartphone with digital camera must implement * this interface. */ interface CameraInterface { sony/dvr/camerainterface.php /** * This is SONY digital camera interface, every sony DVR must implement this interface. */ interface CameraInterface {
Przestrzenie nazw smartphone/camerainterface.php /** * This is a smartphone camera interface, every smartphone with digital camera must implement * this interface. */ interface CameraInterface { sony/dvr/camerainterface.php /** * This is SONY digital camera interface, every sony DVR must implement this interface. */ interface CameraInterface { class IPhone implements PhoneInterface, CameraInterface { public function call($number) { /*... */ public function record() { /*... */
Przestrzenie nazw smartphone/camerainterface.php Kolizja nazw, parser PHP nie wie, który interfejs implementuje nasz IPhone /** * This is a smartphone camera interface, every smartphone with digital camera must implement * this interface. */ interface CameraInterface { sony/dvr/camerainterface.php /** * This is SONY digital camera interface, every sony DVR must implement this interface. */ interface CameraInterface { class IPhone implements PhoneInterface, CameraInterface { public function call($number) { /*... */ public function record() { /*... */
Przestrzenie nazw src/apple/camerainterface.php namespace Apple; interface CameraInterface { public function record(); public function takephoto ();
Przestrzenie nazw src/apple/camerainterface.php namespace Apple; interface CameraInterface { public function record(); public function takephoto (); src/apple/iphone.php namespace Apple; class Iphone implements CameraInterface {
Przestrzenie nazw namespace MyApplication ; use Apple; $phone = new Apple\IPhone ();
http://www.phptherightway.com/
Zarządzanie zależnościami composer
Zarządzanie zależnościami co to są zależności composer.json packagist automatyczne ładowanie klas
Zależności i zależności zależności
composer.json { "require": { "symfony/translation": "*"
https://packagist.org/
Automatyczne ładowanie klas require 'vendor/autoload.php' ; use Symfony\Component\Translation\Translator ; use Symfony\Component\Translation\MessageSelector ; use Symfony\Component\Translation\Loader\ArrayLoader ; $translator = new Translator ('fr_fr', new MessageSelector ()); $translator ->setfallbacklocales (array('fr')); $translator ->addloader ('array', new ArrayLoader ()); $translator ->addresource ('array', array( 'Hello World!' => 'Bonjour', ), 'fr'); echo $translator ->trans('hello World!' )."\n";
composer.json { "require": { "symfony/translation": "*", "autoload": { "psr-4": {"RSTDevSchool\\": "src/"
Automatyczne ładowanie klas namespace RSTDevSchool ; use Symfony\Component\Translation\Translator ; use Symfony\Component\Translation\MessageSelector ; use Symfony\Component\Translation\Loader\ArrayLoader ; class Renderer { public function render($view) { /* */ $this->renderinternal ($view, $this->translator );
Automatyczne ładowanie klas require 'vendor/autoload.php' ; use \RstDevSchool\Renderer ;
Automatyczne ładowanie klas require 'vendor/autoload.php' ; use \RstDevSchool\Renderer ; use \RSTDevSchool as Rst;
Automatyczne ładowanie klas require 'vendor/autoload.php' ; use \RstDevSchool\Renderer ; use \RSTDevSchool as Rst; $renderer = new Rst/Renderer (); $renderer ->render('myview' );
Automatyczne ładowanie klas require 'vendor/autoload.php' ; use \RstDevSchool\Renderer ; use \RSTDevSchool as Rst; $renderer = new Rst/Renderer (); $renderer ->render('myview' ); $renderer = new Renderer (); $renderer ->render('myotherview' );
slim framework { "require": { "slim/slim": "2.*"
slim framework require 'vendor/autoload.php' ; $app = new \Slim\Slim (); $app->get('/hello/:name', function ($name) { echo "Hello, $name"; ); $app->run();
Dziekuje ;)