Introduction to PHP
OOP in PHP
Week 11, day1
What is OOP?
• Object-oriented programming (OOP) is a style of programming
that focuses on using objects to design and bui...
Java PHP
class shape
{
private int width;
private int height;
public int calculateArea()
{
return width * height;
}
}
clas...
Java PHP
class shape
{
private int width;
private int height;
public int calculateArea()
{
return width * height;
}
}
shap...
Objects in detail
• An object is always passed by reference rather than by value.
example:
$a = new shape();
$b = $a();
In...
Inheritance in PHP
• One of the key fundamental concepts of OOP is inheritance.
• This allows a class to extend another cl...
class a {
function test()
{
echo "a::test called";
}
function func()
{
echo "a::func called";
}
}
class b extends a {
func...
class a {
function test()
{
echo "a::test called";
}
function func()
{
echo "a::func called";
}
}
class b extends a {
func...
class a {
function test()
{
echo "a::test called";
}
function func()
{
echo "a::func called";
}
}
class b extends a {
func...
class a {
function test()
{
echo "a::test called";
}
function func()
{
echo "a::func called";
}
}
class b extends a {
func...
$this keyword
class myClass {
function myFunction() {
echo "You called myClass::myFunction";
}
function callMyFunction() {...
$this ketword
class myClass {
function myFunction($data) {
echo "The value is $data";
}
function callMyFunction($data) {
$...
Constructors
• The constructor and destructor are special class methods that are called, as
their names suggest, on object...
Constructor having same name of class
class foo {
function foo()
{
// PHP 4 style constructor
}
}
new foo();
Constructor having same name of class
class foo {
function foo()
{
// PHP 4 style constructor
}
}
new foo();
This style of...
Constructor using Magic Methods
class foo {
function __construct()
{
echo “constructor”;
}
}
new foo();
Constructor using Magic Methods
class foo {
function __construct()
{
echo “constructor”;
}
}
new foo();
• Magic methods ar...
Destructors
• Destructors are called right before an object is destroyed, and is
useful for performing cleanup procedures—...
Visibility
• public : The resource can be accessed from any scope.
• protected : The resource can only be accessed from wi...
class foo {
public $foo = ’bar’;
protected $baz = ’bat’;
private $qux = ’bingo’;
function __construct()
{
var_dump(get_obj...
class foo {
public $foo = ’bar’;
protected $baz = ’bat’;
private $qux = ’bingo’;
function __construct()
{
var_dump(get_obj...
class foo {
public $foo = ’bar’;
protected $baz = ’bat’;
private $qux = ’bingo’;
function __construct()
{
var_dump(get_obj...
class foo {
public $foo = ’bar’;
protected $baz = ’bat’;
private $qux = ’bingo’;
function __construct()
{
var_dump(get_obj...
class foo {
public $foo = ’bar’;
protected $baz = ’bat’;
private $qux = ’bingo’;
function __construct()
{
var_dump(get_obj...
Static Methods and Properties
• Unlike regular methods and properties, static methods and properties are
accessible as par...
Static Methods and Properties
class foo {
static $bar = "bat";
static public function baz()
{
echo "Hello World";
}
}
$a= ...
Static Methods and Properties
class foo {
static $bar = "bat";
static public function baz()
{
echo "Hello World";
}
}
$a= ...
Static Methods and Properties
class foo {
static $bar = "bat";
static public function baz()
{
echo "Hello World";
}
}
$a= ...
Class Constants
• Class constants work in the same way as regular constants, except
they are scoped within a class. Class ...
Abstract Classes
• An abstract class essentially defines the basic skeleton of a specific
type of encapsulated entity—for ...
Abstract Classes
abstract class DataStore_Adapter {
private $id;
abstract function insert();
abstract function update();
p...
Interfaces
• Interfaces, on the other hand, are used to specify an API that a
class must implement.
• This allows you to c...
Interfaces
interface DataStore_Adapter {
public function insert();
public function update();
public function save();
publi...
Determining An Object’s Class
• It is often convenient to be able to determine whether a given object is an
instance of a ...
Questions?
“A good question deserve a good grade…”
End of day
If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.co...
Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
NC Complex, N...
Upcoming SlideShare
Loading in...5
×

Introduction to php oop

643

Published on

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

No Downloads
Views
Total Views
643
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
76
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Introduction to php oop

  1. 1. Introduction to PHP OOP in PHP Week 11, day1
  2. 2. What is OOP? • Object-oriented programming (OOP) is a style of programming that focuses on using objects to design and build applications. • Class : is the base design of objects • Object : is the instance of a class • No memory is allocated when a class is created. • Memory is allocated only when an object is created.
  3. 3. Java PHP class shape { private int width; private int height; public int calculateArea() { return width * height; } } class shape { private $width; private $height; public function calculateArea() { return $width * $height; } } Classes
  4. 4. Java PHP class shape { private int width; private int height; public int calculateArea() { return width * height; } } shape rectangle = new shape(); class shape { private $width; private $height; public function calculateArea() { return $width * $height; } } $rectangle = new shape(); Objects
  5. 5. Objects in detail • An object is always passed by reference rather than by value. example: $a = new shape(); $b = $a(); In this case, both $a and $b will point to the same object, even though we didn’t specify that we wanted this to happen by means of any special syntax. 1000 1000 Heap 1000$a $b
  6. 6. Inheritance in PHP • One of the key fundamental concepts of OOP is inheritance. • This allows a class to extend another class, essentially adding new methods and properties, as well as overriding existing ones as needed. Example as follows
  7. 7. class a { function test() { echo "a::test called"; } function func() { echo "a::func called"; } } class b extends a { function test() { echo "b::test called"; } } class c extends b { function test() { parent::test(); } } class d extends c { function test() { b::test(); } } $a = new a(); $b = new b(); $c = new c(); $d = new d(); $a->test(); // Outputs "a::test called" $b->test(); // Outputs "b::test called" $b->func(); // Outputs "a::func called" $c->test(); // Outputs "b::test called" $d->test(); // Outputs "b::test called"
  8. 8. class a { function test() { echo "a::test called"; } function func() { echo "a::func called"; } } class b extends a { function test() { echo "b::test called"; } } class c extends b { function test() { parent::test(); } } class d extends c { function test() { b::test(); } } $a = new a(); $b = new b(); $c = new c(); $d = new d(); $a->test(); // Outputs "a::test called" $b->test(); // Outputs "b::test called" $b->func(); // Outputs "a::func called" $c->test(); // Outputs "b::test called" $d->test(); // Outputs "b::test called" Parent keyword is used to access the immediate parents properties and methods
  9. 9. class a { function test() { echo "a::test called"; } function func() { echo "a::func called"; } } class b extends a { function test() { echo "b::test called"; } } class c extends b { function test() { parent::test(); } } class d extends c { function test() { b::test(); } } $a = new a(); $b = new b(); $c = new c(); $d = new d(); $a->test(); // Outputs "a::test called" $b->test(); // Outputs "b::test called" $b->func(); // Outputs "a::func called" $c->test(); // Outputs "b::test called" $d->test(); // Outputs "b::test called" We can use the parent class’s name for accessing its properties and methods
  10. 10. class a { function test() { echo "a::test called"; } function func() { echo "a::func called"; } } class b extends a { function test() { echo "b::test called"; } } class c extends b { function test() { parent::test(); } } class d extends c { function test() { b::test(); } } $a = new a(); $b = new b(); $c = new c(); $d = new d(); $a->test(); // Outputs "a::test called" $b->test(); // Outputs "b::test called" $b->func(); // Outputs "a::func called" $c->test(); // Outputs "b::test called" $d->test(); // Outputs "b::test called" From outside the scope of a class, its methods are called using the indirection operator ->
  11. 11. $this keyword class myClass { function myFunction() { echo "You called myClass::myFunction"; } function callMyFunction() { // ??? } } Suppose we have a scenario where one of the function inside a class require to call another function inside the same class
  12. 12. $this ketword class myClass { function myFunction($data) { echo "The value is $data"; } function callMyFunction($data) { $this->myFunction($data); } } $obj = new myClass(); $obj->callMyFunction(123); //This will output The value is 123. We can acheive it by using a special keyword called “$this”
  13. 13. Constructors • The constructor and destructor are special class methods that are called, as their names suggest, on object creation and destruction, respectively. • We can define a constructor in two ways 1. By defining a method with same name as of the class name 2. By defining a __construct() Magic Method
  14. 14. Constructor having same name of class class foo { function foo() { // PHP 4 style constructor } } new foo();
  15. 15. Constructor having same name of class class foo { function foo() { // PHP 4 style constructor } } new foo(); This style of constructors were used in PHP4 but deprecated by php 5 .Moreover this method has several draw backs—for example, if you decided to rename your class, you would also have to rename your constructor.
  16. 16. Constructor using Magic Methods class foo { function __construct() { echo “constructor”; } } new foo();
  17. 17. Constructor using Magic Methods class foo { function __construct() { echo “constructor”; } } new foo(); • Magic methods are special methods in php which will be invoked by PHP itself upon certain scenarios.magic methods usually starts with “__” •__construct is a magic method which will be invoked by PHP whenever an object of that class is created. •If PHP couldnt find __construct method it will look for the old style php constructor ie a method that has same name as class
  18. 18. Destructors • Destructors are called right before an object is destroyed, and is useful for performing cleanup procedures—such as disconnecting from a remote resource, or deleting temporary files class foo { function __destruct() { echo “destructor”; } } $a=new foo(); unset($a); //Destructor will be called
  19. 19. Visibility • public : The resource can be accessed from any scope. • protected : The resource can only be accessed from within the class where it is defined and its descendants. • private : The resource can only be accessed from within the class where it is defined. • Final : The resource is accessible from any scope, but cannot be overridden in descendant classes. This only applies to methods and classes and not to properties
  20. 20. class foo { public $foo = ’bar’; protected $baz = ’bat’; private $qux = ’bingo’; function __construct() { var_dump(get_object_vars($this)); } } class bar extends foo { function __construct() { var_dump(get_object_vars($this)); } } class baz { function __construct() { $foo = new foo(); var_dump(get_object_vars($foo)); } } new foo(); new bar(); new baz();
  21. 21. class foo { public $foo = ’bar’; protected $baz = ’bat’; private $qux = ’bingo’; function __construct() { var_dump(get_object_vars($this)); } } class bar extends foo { function __construct() { var_dump(get_object_vars($this)); } } class baz { function __construct() { $foo = new foo(); var_dump(get_object_vars($foo)); } } new foo(); new bar(); new baz(); get_object_vars() is a special method which accepts object as argument and returns an array of member properties inside that object
  22. 22. class foo { public $foo = ’bar’; protected $baz = ’bat’; private $qux = ’bingo’; function __construct() { var_dump(get_object_vars($this)); } } class bar extends foo { function __construct() { var_dump(get_object_vars($this)); } } class baz { function __construct() { $foo = new foo(); var_dump(get_object_vars($foo)); } } new foo(); new bar(); new baz(); array(3) { ["foo"]=>string(3) "bar“ ["baz"]=>string(3) "bat" ["qux"]=>string(5) “bingo" }
  23. 23. class foo { public $foo = ’bar’; protected $baz = ’bat’; private $qux = ’bingo’; function __construct() { var_dump(get_object_vars($this)); } } class bar extends foo { function __construct() { var_dump(get_object_vars($this)); } } class baz { function __construct() { $foo = new foo(); var_dump(get_object_vars($foo)); } } new foo(); new bar(); new baz(); array(2) { ["foo"]=>string(3) "bar" ["baz"]=>string(3) "bat" }
  24. 24. class foo { public $foo = ’bar’; protected $baz = ’bat’; private $qux = ’bingo’; function __construct() { var_dump(get_object_vars($this)); } } class bar extends foo { function __construct() { var_dump(get_object_vars($this)); } } class baz { function __construct() { $foo = new foo(); var_dump(get_object_vars($foo)); } } new foo(); new bar(); new baz(); array(1) { ["foo"]=> string(3) "bar" }
  25. 25. Static Methods and Properties • Unlike regular methods and properties, static methods and properties are accessible as part of a class itself, as opposed to existing only within the scope of one of its instances. • Ie there is no need to create objects for accessing the static methods and properties. It can be accessed by using the class name itself
  26. 26. Static Methods and Properties class foo { static $bar = "bat"; static public function baz() { echo "Hello World"; } } $a= new foo(); $a>baz(); echo $foo->bar; foo::baz Notice: Undefined property: foo::$bar in PHPDocument1 on line 17
  27. 27. Static Methods and Properties class foo { static $bar = "bat"; static public function baz() { echo "Hello World"; } } $a= new foo(); $a->baz(); echo $a->bar; Will cause an error as below a::baz Notice: Undefined property: a::$bar in PHPDocument1 on line 17
  28. 28. Static Methods and Properties class foo { static $bar = "bat"; static public function baz() { echo "Hello World"; } } $a= new foo(); foo->baz(); echo foo->bar; //Will display as below Hello World bat
  29. 29. Class Constants • Class constants work in the same way as regular constants, except they are scoped within a class. Class constants are public, and accessible from all scopes; for example, the following script will output Hello World: class foo { const BAR = "Hello World"; } echo foo::BAR;
  30. 30. Abstract Classes • An abstract class essentially defines the basic skeleton of a specific type of encapsulated entity—for example, you can use an abstract class to define the basic concept of “car” as having two doors, a lock and a method that locks or unlocks the doors. • Abstract classes cannot be used directly, but they must be extended so that the descendent class provides a full complement of methods
  31. 31. Abstract Classes abstract class DataStore_Adapter { private $id; abstract function insert(); abstract function update(); public function save() { if (!is_null($this->id)) { $this->update(); } else { $this->insert(); } } } class PDO_DataStore_Adapter extends DataStore_Adapter { public __construct($dsn) { // class’s own method definition } function insert() { // definition of abstract method } function update() { // definition of abstract method } }
  32. 32. Interfaces • Interfaces, on the other hand, are used to specify an API that a class must implement. • This allows you to create a common “contract” that your classes must implement in order to satisfy certain logical requirements
  33. 33. Interfaces interface DataStore_Adapter { public function insert(); public function update(); public function save(); public function newRecord($name = null); } class PDO_DataStore_Adapter implements DataStore_Adapter { public function insert() { // ... } public function update() { // ... } public function save() { // ... } public function newRecord($name = null) { } }
  34. 34. Determining An Object’s Class • It is often convenient to be able to determine whether a given object is an instance of a particular class, or whether it implements a specific interface. This can be done by using the instanceof operator: if ($obj instanceof MyClass) { echo "$obj is an instance of MyClass"; } • Naturally, instanceof allows you to inspect all of the ancestor classes of your object, as well as any interfaces.
  35. 35. Questions? “A good question deserve a good grade…”
  36. 36. End of day
  37. 37. If this presentation helped you, please visit our page facebook.com/baabtra and like it. Thanks in advance. www.baabtra.com | www.massbaab.com |www.baabte.com
  38. 38. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: info@baabtra.com
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×