SlideShare uma empresa Scribd logo
1 de 38
Baixar para ler offline
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 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.
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
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
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
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
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"
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
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
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 ->
$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
$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”
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
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 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.
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 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
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
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
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();
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
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"
}
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"
}
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"
}
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
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
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
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
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;
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
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
}
}
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
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)
{
}
}
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.
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.com | www.massbaab.com |www.baabte.com
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

Mais conteúdo relacionado

Mais procurados

Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPsRavi Bhadauria
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVAAbhilash Nair
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHPLorna Mitchell
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in javakamal kotecha
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classesFajar Baskoro
 
Python unit 3 m.sc cs
Python unit 3 m.sc csPython unit 3 m.sc cs
Python unit 3 m.sc csKALAISELVI P
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingShivam Singhal
 
Variables in python
Variables in pythonVariables in python
Variables in pythonJaya Kumari
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPWildan Maulana
 

Mais procurados (20)

Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
 
PHP- Introduction to Object Oriented PHP
PHP-  Introduction to Object Oriented PHPPHP-  Introduction to Object Oriented PHP
PHP- Introduction to Object Oriented PHP
 
Chapter 05 classes and objects
Chapter 05 classes and objectsChapter 05 classes and objects
Chapter 05 classes and objects
 
Classes, objects in JAVA
Classes, objects in JAVAClasses, objects in JAVA
Classes, objects in JAVA
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
Oops in PHP
Oops in PHPOops in PHP
Oops in PHP
 
Object Oriented Programming in PHP
Object Oriented Programming in PHPObject Oriented Programming in PHP
Object Oriented Programming in PHP
 
Php oop presentation
Php   oop presentationPhp   oop presentation
Php oop presentation
 
Introduction to class in java
Introduction to class in javaIntroduction to class in java
Introduction to class in java
 
PHP Classes and OOPS Concept
PHP Classes and OOPS ConceptPHP Classes and OOPS Concept
PHP Classes and OOPS Concept
 
Lect 1-java object-classes
Lect 1-java object-classesLect 1-java object-classes
Lect 1-java object-classes
 
Python unit 3 m.sc cs
Python unit 3 m.sc csPython unit 3 m.sc cs
Python unit 3 m.sc cs
 
Introduction to OOP(in java) BY Govind Singh
Introduction to OOP(in java)  BY Govind SinghIntroduction to OOP(in java)  BY Govind Singh
Introduction to OOP(in java) BY Govind Singh
 
CLASS & OBJECT IN JAVA
CLASS & OBJECT  IN JAVACLASS & OBJECT  IN JAVA
CLASS & OBJECT IN JAVA
 
encapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloadingencapsulation, inheritance, overriding, overloading
encapsulation, inheritance, overriding, overloading
 
Variables in python
Variables in pythonVariables in python
Variables in python
 
OOPS Basics With Example
OOPS Basics With ExampleOOPS Basics With Example
OOPS Basics With Example
 
Class introduction in java
Class introduction in javaClass introduction in java
Class introduction in java
 
Chapter 07 inheritance
Chapter 07 inheritanceChapter 07 inheritance
Chapter 07 inheritance
 
Object Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOPObject Oriented Programming with PHP 5 - More OOP
Object Oriented Programming with PHP 5 - More OOP
 

Destaque

S.O.L.I.D. Principles for Software Architects
S.O.L.I.D. Principles for Software ArchitectsS.O.L.I.D. Principles for Software Architects
S.O.L.I.D. Principles for Software ArchitectsRicardo Wilkins
 
SOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principlesSOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principlesSergey Karpushin
 
Object Oriented Design SOLID Principles
Object Oriented Design SOLID PrinciplesObject Oriented Design SOLID Principles
Object Oriented Design SOLID Principlesrainynovember12
 
"SOLID" Object Oriented Design Principles
"SOLID" Object Oriented Design Principles"SOLID" Object Oriented Design Principles
"SOLID" Object Oriented Design PrinciplesSerhiy Oplakanets
 

Destaque (20)

Algorithm flowcharts
Algorithm flowchartsAlgorithm flowcharts
Algorithm flowcharts
 
CSS (Cascading Style Sheets)
CSS (Cascading Style Sheets)CSS (Cascading Style Sheets)
CSS (Cascading Style Sheets)
 
Intoduction to php arrays
Intoduction to php arraysIntoduction to php arrays
Intoduction to php arrays
 
Oop concept
Oop conceptOop concept
Oop concept
 
Normalization,ddl,dml,dcl
Normalization,ddl,dml,dclNormalization,ddl,dml,dcl
Normalization,ddl,dml,dcl
 
Java script operators
Java script operatorsJava script operators
Java script operators
 
Introduction to mysql part 6
Introduction to mysql part 6Introduction to mysql part 6
Introduction to mysql part 6
 
JQuery
JQueryJQuery
JQuery
 
Oops in c
Oops in cOops in c
Oops in c
 
Python
PythonPython
Python
 
Database and types of databases
Database and types of databasesDatabase and types of databases
Database and types of databases
 
Scope of variable
Scope of variableScope of variable
Scope of variable
 
Creating a web service
Creating a web serviceCreating a web service
Creating a web service
 
Interpreted and compiled language
Interpreted and compiled languageInterpreted and compiled language
Interpreted and compiled language
 
S.O.L.I.D. Principles for Software Architects
S.O.L.I.D. Principles for Software ArchitectsS.O.L.I.D. Principles for Software Architects
S.O.L.I.D. Principles for Software Architects
 
SOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principlesSOLID, DRY, SLAP design principles
SOLID, DRY, SLAP design principles
 
Object Oriented Design SOLID Principles
Object Oriented Design SOLID PrinciplesObject Oriented Design SOLID Principles
Object Oriented Design SOLID Principles
 
Introduction to java and oop
Introduction to java and oopIntroduction to java and oop
Introduction to java and oop
 
Introduction to php web programming - get and post
Introduction to php  web programming - get and postIntroduction to php  web programming - get and post
Introduction to php web programming - get and post
 
"SOLID" Object Oriented Design Principles
"SOLID" Object Oriented Design Principles"SOLID" Object Oriented Design Principles
"SOLID" Object Oriented Design Principles
 

Semelhante a Introduction to php oop

Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)Bozhidar Boshnakov
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHPwahidullah mudaser
 
Advanced php
Advanced phpAdvanced php
Advanced phphamfu
 
FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3Toni Kolev
 
Practical PHP 5.3
Practical PHP 5.3Practical PHP 5.3
Practical PHP 5.3Nate Abele
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindiappsdevelopment
 
PHP Language Trivia
PHP Language TriviaPHP Language Trivia
PHP Language TriviaNikita Popov
 
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)James Titcumb
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Developmentjsmith92
 
Demystifying Object-Oriented Programming #ssphp16
Demystifying Object-Oriented Programming #ssphp16Demystifying Object-Oriented Programming #ssphp16
Demystifying Object-Oriented Programming #ssphp16Alena Holligan
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxLecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxDavidLazar17
 
Php 5.4: New Language Features You Will Find Useful
Php 5.4: New Language Features You Will Find UsefulPhp 5.4: New Language Features You Will Find Useful
Php 5.4: New Language Features You Will Find UsefulDavid Engel
 
The Origin of Lithium
The Origin of LithiumThe Origin of Lithium
The Origin of LithiumNate Abele
 
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...James Titcumb
 

Semelhante a Introduction to php oop (20)

Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHP (part 1)
 
Object Oriented Programming in PHP
Object Oriented Programming  in PHPObject Oriented Programming  in PHP
Object Oriented Programming in PHP
 
Advanced php
Advanced phpAdvanced php
Advanced php
 
FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3FFW Gabrovo PMG - PHP OOP Part 3
FFW Gabrovo PMG - PHP OOP Part 3
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
PHP OOP
PHP OOPPHP OOP
PHP OOP
 
Ch8(oop)
Ch8(oop)Ch8(oop)
Ch8(oop)
 
Practical PHP 5.3
Practical PHP 5.3Practical PHP 5.3
Practical PHP 5.3
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in php
 
OOP in PHP.pptx
OOP in PHP.pptxOOP in PHP.pptx
OOP in PHP.pptx
 
PHP Language Trivia
PHP Language TriviaPHP Language Trivia
PHP Language Trivia
 
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
Mirror, mirror on the wall: Building a new PHP reflection library (DPC 2016)
 
SPL: The Missing Link in Development
SPL: The Missing Link in DevelopmentSPL: The Missing Link in Development
SPL: The Missing Link in Development
 
OOPs Concept
OOPs ConceptOOPs Concept
OOPs Concept
 
Demystifying Object-Oriented Programming #ssphp16
Demystifying Object-Oriented Programming #ssphp16Demystifying Object-Oriented Programming #ssphp16
Demystifying Object-Oriented Programming #ssphp16
 
Lecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptxLecture 17 - PHP-Object-Orientation.pptx
Lecture 17 - PHP-Object-Orientation.pptx
 
Php 5.4: New Language Features You Will Find Useful
Php 5.4: New Language Features You Will Find UsefulPhp 5.4: New Language Features You Will Find Useful
Php 5.4: New Language Features You Will Find Useful
 
The Origin of Lithium
The Origin of LithiumThe Origin of Lithium
The Origin of Lithium
 
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
Mirror, mirror on the wall - Building a new PHP reflection library (Nomad PHP...
 
Oops in php
Oops in phpOops in php
Oops in php
 

Mais de baabtra.com - No. 1 supplier of quality freshers

Mais de baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 

Último

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...shyamraj55
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 

Último (20)

Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
Automating Business Process via MuleSoft Composer | Bangalore MuleSoft Meetup...
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 

Introduction to php oop

  • 1. Introduction to PHP OOP in PHP Week 11, day1
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. $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. $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. 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. Constructor having same name of class class foo { function foo() { // PHP 4 style constructor } } new foo();
  • 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. Constructor using Magic Methods class foo { function __construct() { echo “constructor”; } } new foo();
  • 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. Questions? “A good question deserve a good grade…”
  • 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. 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