PHP: Hypertext
Preprocessor
PHP - OOP
@d_danailov
Topics Today
●
●
●
●
●
●

Object-oriented programming Overview
Objects & Classes
Abstraction and encapsulation
Inheritance...
OOP
Object-oriented programming (OOP) is a programming
paradigm that represents concepts as "objects" that have
data field...
OOP (2)
●
●
●
●

Objects & Classes
Abstraction and encapsulation
Inheritance
Polymorphism
Objects & Classes
What is an object
Objects are the elements through which we perceive the
world around us. All objects have these character...
<?php
//Objects
$human = new stdClass();
$human->gender = 'm';
$human->age = 35;
$human->name = 'Todor Dimov';
$child1 = n...
Classes (classification of objects)
A class is a group of objects with same attributes and
behavior. The characteristics o...
<?php
/* Classes */
class Human {
private $name = null;
private $gender = null;
private $age = null;
private $childrens = ...
<?php
/* ... */
$child1 = new Human('Dimo Todorov', 'm', 12);
$child2 = new Human('Todorka Todorova', 'f', 16);
$childrens...
Abstraction and
encapsulation
Abstraction
In computer science, abstraction is the process by which
data and programs are defined with a representation
s...
Encapsulation
Encapsulation is the practice of including in an object
everything it needs hidden from the other objects in...
Inheritance
Inheritance
In object-oriented programming (OOP), inheritance is a
way to establish Is-a relationships between objects. In...
// Inheritance
class Human
{
private $name = null;
private $gender = null;
private $age = null;
public function __construc...
class ParentClass extends Human {
private $name = null;
private $gender = null;
private $age = null;
private $childrens = ...
Class Child extends Human {
private $parents = array();
public function __construct($name, $gender, $age) {
$this->name = ...
Polymorphism
Polymorphism
Polymorphism describes a pattern in object oriented
programming in which classes have different functionality...
Abstract Classes
Class Abstraction
PHP 5 introduces abstract classes and methods. Classes
defined as abstract may not be instantiated, and ...
<?php
abstract class AbstractHuman {
private $name = null;
private $gender = null;
private $age = null;
public function __...
<?php
class ParentClass extends AbstractHuman {
public function getName() {
return $this->name;
}
public function setName(...
Interfaces
Object Interfaces
Object interfaces allow you to create code which specifies
which methods a class must implement, without...
<?php
// Interfaces
interface iHuman {
public function getName();
public function setName($name);
public function getGende...
<?php
class ParentClass implements iHuman {
public function getName() {
return $this->name;
}
public function setName($nam...
Class Diagrams
Questions ?
Dimitar Danailov
Senior Developer at 158ltd.com
dimityr.danailov[at]gmail.com
Github
Slide Share
Founder at Va...
VFU SEM - PHP OOP [12.10.2013]
Upcoming SlideShare
Loading in...5
×

VFU SEM - PHP OOP [12.10.2013]

1,498

Published on

Course Website: http://sem.vfu.bg
Github: https://github.com/dimitardanailov/sem-vfu-programming
Google Docs: https://docs.google.com/presentation/d/1rCi7LaxdDc9-cbaSNSDgUkVv208iwFw559sIgJ2Kr1E/edit

Object-oriented programming (OOP) is a programming paradigm that represents concepts as "objects" that have data fields (attributes that describe the object) and associated procedures known as methods. Objects, which are usually instances of classes, are used to interact with one another to design applications and computer programs. C++, Objective-C, Smalltalk, Java and C# are examples of object-oriented programming languages.

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
1,498
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
13
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

VFU SEM - PHP OOP [12.10.2013]

  1. 1. PHP: Hypertext Preprocessor PHP - OOP @d_danailov
  2. 2. PHP : Intro Dimitar Danailov Senior Developer at 158ltd.com dimityr.danailov[at]gmail.com Github Slide Share Founder at VarnaIT Senior Developer at 158ltd.com
  3. 3. Topics Today ● ● ● ● ● ● Object-oriented programming Overview Objects & Classes Abstraction and encapsulation Inheritance Polymorphism Class Diagrams
  4. 4. OOP Object-oriented programming (OOP) is a programming paradigm that represents concepts as "objects" that have data fields (attributes that describe the object) and associated procedures known as methods. Objects, which are usually instances of classes, are used to interact with one another to design applications and computer programs. C++, Objective-C, Smalltalk, Java and C# are examples of object-oriented programming languages.
  5. 5. OOP (2) ● ● ● ● Objects & Classes Abstraction and encapsulation Inheritance Polymorphism
  6. 6. Objects & Classes
  7. 7. What is an object Objects are the elements through which we perceive the world around us. All objects have these characteristics : ● Identity ● State ● Behaviour
  8. 8. <?php //Objects $human = new stdClass(); $human->gender = 'm'; $human->age = 35; $human->name = 'Todor Dimov'; $child1 = new stdClass(); $child1->name = 'Dimo Todorov'; $child2 = new stdClass(); $child2->name = 'Todorka Todorova'; $human->childrens = array($child1, $child2); var_dump($human); ?>
  9. 9. Classes (classification of objects) A class is a group of objects with same attributes and behavior. The characteristics of a class are : ● A name ● Attributes ● Methods / Functions
  10. 10. <?php /* Classes */ class Human { private $name = null; private $gender = null; private $age = null; private $childrens = array(); public function __construct($name, $gender, $age, $childrens = array()) { $this->name = $name; $this->gender = $gender; $this->age = $age; $this->childrens = $childrens; } } /* … */ ?>
  11. 11. <?php /* ... */ $child1 = new Human('Dimo Todorov', 'm', 12); $child2 = new Human('Todorka Todorova', 'f', 16); $childrens = array($child1, $child2); $human = new Human('Todor Dimov', 'm', 35, $childrens); var_dump($human); ?>
  12. 12. Abstraction and encapsulation
  13. 13. Abstraction In computer science, abstraction is the process by which data and programs are defined with a representation similar in form to its meaning (semantics), while hiding away the implementation details. Abstraction tries to reduce and factor out details so that the programmer can focus on a few concepts at a time. A system can have several abstraction layers whereby different meanings and amounts of detail are exposed to the programmer.
  14. 14. Encapsulation Encapsulation is the practice of including in an object everything it needs hidden from the other objects in the system.
  15. 15. Inheritance
  16. 16. Inheritance In object-oriented programming (OOP), inheritance is a way to establish Is-a relationships between objects. In classical inheritance where objects are defined by classes, classes can inherit attributes and behavior from pre-existing classes called base classes, superclasses, or parent classes.
  17. 17. // Inheritance class Human { private $name = null; private $gender = null; private $age = null; public function __construct($name, $gender, $age) { $this->name = $name; $this->gender = $gender; $this->age = $age; } static function prinHello($name) { echo 'Hello, ' . $name; } private function greetings() { echo 'Greetings'; } }
  18. 18. class ParentClass extends Human { private $name = null; private $gender = null; private $age = null; private $childrens = array(); public function __construct ($name, $gender, $age) { $this->name = $name; $this->gender = $gender; $this->age = $age; } public function getName() { //$this->greetings(); return parent::prinHello($this->name); } public function getChildrens () { return $this->childrens; } public function setChildren (Child $child) { $this->childrens[] = $child; } }
  19. 19. Class Child extends Human { private $parents = array(); public function __construct($name, $gender, $age) { $this->name = $name; $this->gender = $gender; $this->age = $age; } public function getParents() { return $this->parents; } public function setParent(ParentClass $parent) { $this->parents[] = $parent; } }
  20. 20. Polymorphism
  21. 21. Polymorphism Polymorphism describes a pattern in object oriented programming in which classes have different functionality while sharing a common interface.
  22. 22. Abstract Classes
  23. 23. Class Abstraction PHP 5 introduces abstract classes and methods. Classes defined as abstract may not be instantiated, and any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature - they cannot define the implementation.
  24. 24. <?php abstract class AbstractHuman { private $name = null; private $gender = null; private $age = null; public function __construct ($name, $gender, $age) { $this->name = $name; $this->gender = $gender; $this->age = $age; } abstract public function getName(); abstract public function setName($name); abstract public function getGender (); abstract public function setGender ($gender); abstract public function getAge(); abstract public function setAge($age); } ?>
  25. 25. <?php class ParentClass extends AbstractHuman { public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } public function getGender () { return $this->gender; } public function setGender ($gender) { $this->gender = $gender; } // ... } ?>
  26. 26. Interfaces
  27. 27. Object Interfaces Object interfaces allow you to create code which specifies which methods a class must implement, without having to define how these methods are handled. Interfaces are defined using the interface keyword, in the same way as a standard class, but without any of the methods having their contents defined. All methods declared in an interface must be public, this is the nature of an interface.
  28. 28. <?php // Interfaces interface iHuman { public function getName(); public function setName($name); public function getGender(); public function setGender($gender); public function getAge(); public function setAge($age); } ?>
  29. 29. <?php class ParentClass implements iHuman { public function getName() { return $this->name; } public function setName($name) { $this->name = $name; } public function getGender() { return $this->gender; } public function setGender($gender) { $this->gender = $gender; } // ... } ?>
  30. 30. Class Diagrams
  31. 31. Questions ? Dimitar Danailov Senior Developer at 158ltd.com dimityr.danailov[at]gmail.com Github Slide Share Founder at VarnaIT Senior Developer at 158ltd.com

×