Title: OOPS - concepts in php
1 OOPS -Concepts in PHP
Call US 011-65164822
CPD TECHNOLOGIES
Add- Block C 9/8, Sector-7, Rohini, Delhi
-110085
www.cpd-india.com
www.blog.cpd-india.com
2(No Transcript)
3Topics to be covered
- Interfaces
- Abstract Classes
- Static Methods And Properties
- Accessor Methods
- Determining A Object's Class
- What is OOPS
- Class Objects
- Modifier
- Constructor
- Deconstructor
- Class Constants
- Inheritance In Php
- Magic Function
- Polymorphism
4What Is Oop ?
- Object Oriented Programming (OOP) is the
programming method that involves the use of the
data , structure and organize classes of an
application. The data structure becomes an
objects that includes both functions and data. A
relationship between one object and other object
is created by the programmer
www.cpd-india.com
5Classes
- A class is a programmer defined datatype which
include local fuctions as well as local data. - Like a pattern or a blueprint a oops class has
exact specifications. The specification is the
class' s contract.
6Object
An object is a like a container that contains
methods and properties which are require to make
a certain data types useful. An objects methods
are what it can do and its properties are what it
knows.
7Modifier
8Modifier
- In object oriented programming, some Keywords
are private and some are public in class. These
keyword are known as modifier. These keywords
help you to define how these variables and
properties will be accessed by the user of this
class.
www.cpd-india.com
9Modifier
- Private Properties or methods declared as
private are not allowed to be called from outside
the class. However any method inside the same
class can access them without a problem. In our
Emailer class we have all these properties
declared as private, so if we execute the
following code we will find an error.
www.cpd-india.com
10Modifier
- lt?
- include_once("class.emailer.php")
- emobject new Emailer("hasin_at_somewherein.net")
- emobject-gtsubject "Hello world"
- ?gt
The above code upon execution gives a fatal error
as shown below ltbgtFaprivate property
emailersubject in ltbgtC\OOP with
PHP5\Codes\ch1\class.emailer.phplt/bgt on line
ltbgt43lt/gtltbr /gt tal errorlt/bgt Cannot access That
means you can't access
www.cpd-india.com
11Modifier
- Public Any property or method which is not
explicitly declared as private or protected is a
public method. You can access a public method
from inside or outside the class. - Protected This is another modifier which has a
special meaning in OOP. If any property or method
is declared as protected, you can only access the
method from its subclass. To see how a protected
method or property actually works, we'll use the
following example
12Modifier
- To start, let's open class.emailer.php file (the
Emailer class) and change the declaration of the
sender variable. Make it as follows - protected sender
- Now create another file name class.extendedemailer
.php with the following code
www.cpd-india.com
13Modifier
- lt?
- class ExtendedEmailer extends emailer
-
- function __construct()
- public function setSender(sender)
-
- this-gtsender sender
-
-
- ?gt
www.cpd-india.com
14Constructor Destructor
- Constructor is Acclimated to Initialize the
Object. - Arguments can be taken by constructor.
- A class name has same name as Constructor.
- Memory allocation is done by Constructor.
www.cpd-india.com
15Constructor Destructor
- The objects that are created in memory, are
destroyed by the destructor. - Arguments can be taken by the destructor.
- Overloading is possible in destructor.
- It has same name as class name with tiled
operator.
16ClassContants
17Class Constants
- You can make constants in your PHP scripts
utilizing the predefine keyword to define
(constant name, constant value). At the same time
to make constants in the class you need to
utilize the const keyword. These constants really
work like static variables, the main distinction
is that they are read-only.
www.cpd-india.com
18Class Constants
- lt?
- class WordCounter
-
- const ASC1 //you need not use sign before
Constants - const DESC2
- private words
- function __construct(filename)
-
- file_content file_get_contents(filename)
- this-gtwords
- (array_count_values(str_word_count(strtolower
- (file_content),1)))
www.cpd-india.com
19Class Constants
-
- public function count(order)
-
- if (orderselfASC)
- asort(this-gtwords)
- else if(orderselfDESC)
- arsort(this-gtwords)
- foreach (this-gtwords as keygtval)
- echo key ." ". val."ltbr/gt"
-
-
- ?gt
www.cpd-india.com
20Inheritance
21Inheritance In Php
- Inheritance is a well-known programming rule, and
PHP makes utilization of this standard in its
object model. This standard will influence the
way numerous objects and classes identify with
each other. - For illustration, when you extend a class, the
subclass inherits each public and protected
method from the guardian class. Unless a class
overrides those techniques, they will hold their
unique functionality
www.cpd-india.com
22Inheritance
23Magic Functions
24Magic Functions
- There are some predefine function names which
you cant use in your programme unless you have
magic functionality relate with them. These
functions are known as Magic Functions. Magic
functions have special names which begine with
two underscores.
www.cpd-india.com
25Magic Functions
- _ _construct()
- _ _deconstruct()
- _ _call()
- _ _callStatic()
- _ _get()
- _ _set()
- _ _isset()
- _ _unset()
- __sleep()
- __wakeup()
- __tostring()
- __invoke()
- __ set_state()
- __ clone()
- __ debugInfo()
www.cpd-india.com
26Magic Functions
- _ _construct()
- Construct function is called when object is
instantiated. Generally it is used in php 5 for
creating constructor - _ _deconstruct()
- It is the opposite of construct function. When
object of a class is unset, this function is
called.
www.cpd-india.com
27Magic Functions
- _ _call()
- When a class in a function is try to call an
accessible or inaccessible function , this method
is called. - _ _callStatic()
- It is similar to __callStatic() with only one
difference that is its triggered when you try to
call an accessible or inaccessible function in
static context.
www.cpd-india.com
28Magic Functions
- _ _get()
- This function is triggered when your object try
call a variable of a class which is either
unavailable or inaccessible. - _ _set()
- This function is called when we try to change to
value of a property which is unavailable or
inaccessible. -
www.cpd-india.com
29Polymorphism
www.cpd-india.com
30Polymorphism
- The ability of a object, variable or function to
appear in many form is known as polymorphism. It
allows a developer to programme in general rather
than programme in specific. There are two types
of polymorphism. - compile time polymorphism
- run-time polymorphism".
www.cpd-india.com
31Interfaces
32Interfaces
- There are some specific set of variable and
functions which can be called outside a class
itself. These are known as interfaces. Interfaces
are declared using interface keyword. - lt?
- //interface.dbdriver.php
- interface DBDriver
-
- public function connect()
- public function execute(sql)
-
- ?gt
www.cpd-india.com
33Abstract Classes
34Abstract Classes
- A class which is declared using abstract keyword
is known as abstract class. An abstract class is
not implemented just declared only (followed by
semicolon without braces) - lt?
- //abstract.reportgenerator.php
- abstract class ReportGenerator
-
- public function generateReport(resultArray)
-
- //write code to process the multidimensional
result array and - //generate HTML Report
-
-
- ?
www.cpd-india.com
35Static Method And Properties
36Static Methods Properties
- In object oriented programming, static keyword is
very crucial. Static properties and method acts
as a significant element in design pattern and
application design. To access any method or
attribute in a class you must create an instance
(i.e. using new keyword, like object new
emailer()), otherwise you can't access them. But
there is a difference for static methods and
properties. You can access a static method or
property directly without creating any instance
of that class. A static member is like a global
member for that class and all instances of that
class
37Static Methods Properties
- lt?
- //class.dbmanager.php
- class DBManager
-
- public static function getMySQLDriver()
-
- //instantiate a new MySQL Driver object and
return -
- public static function getPostgreSQLDriver()
www.cpd-india.com
38Static Methods Properties
-
- //instantiate a new PostgreSQL Driver object and
return -
- public static function getSQLiteDriver()
-
- //instantiate a new MySQL Driver object and
return -
-
- ?gt
www.cpd-india.com
39Accessor Method
40Accessor Method
- Accessor methods are simply methods that are
solely devoted to get and set the value of any
class properties. It's a good practice to access
class properties using accessor methods instead
of directly setting or getting their value.
Though accessor methods are the same as other
methods, there are some conventions writing them.
There are two types of accessor methods. One is
called getter, whose purpose is returning value
of any class property. The other is setter that
sets a value into a class property.
www.cpd-india.com
41Accessor Method
- lt?
- class Student
-
- private name
- private roll
- More free ebooks http//fast-file.blogspot.com
- Chapter 2
- 39
- public function setName(name)
-
- this-gtname name
-
www.cpd-india.com
42Accessor Method
- public function setRoll(roll)
-
- this-gtroll roll
-
- public function getName()
-
- return this-gtname
-
- public function getRoll()
-
- return this-gtroll
-
-
- ?gt
www.cpd-india.com
43Determining A Object's Class
- public function setRoll(roll)
-
- this-gtroll roll
-
- public function getName()
-
- return this-gtname
-
- public function getRoll()
-
- return this-gtroll
-
-
- ?gt
www.cpd-india.com
44CPD TECHNOLOGIES
Block C 9/8, Sector-7, Rohini, Delhi -110085
support_at_cpd-india.com