Sunteți pe pagina 1din 13

OOPSIN PHP

facebook.com/apex.tgi
twitter.com/ApextgiNoida
pinterest.com/apextgi
Class
Basic class defnitions begin with the keyword class, followed by a class name, followed by a pair of curly
braces which enclose the defnitions of the properties and methods belonging to the class.
The class name can be any valid label which is not a PHP reserved word. valid class name starts with a
letter or underscore, followed by any number of letters, numbers, or underscores.
class may contain its own constants, variables !called "properties"#, and functions !called "methods"#.
#Simple Class defnition
$%php
class &impleClass
'
(( property declaration
public )var * +a default value+,
(( method declaration
public function display-ar!# '
echo )this./var,
0
0
%/
1nstance of a class
To create an instance of a class, the new keyword must be used. n ob2ect will always be created unless the ob2ect has a constructor
defned that throws an e3ception on error. Classes should be defned before instantiation !and in some cases this is a re4uirement#.
1f a string containing the name of a class is used with new, a new instance of that class will be created. 1f the class is in a namespace, its
fully 4ualifed name must be used when doing this.
#Creating an instance
$%php
)instance * new &impleClass!#,
(( This can also be done with a variable5
)class6ame * +7oo+,
)instance * new )class6ame!#, (( 7oo!#
%/
Properties

Class member variables are called "properties". 8ou may also see them referred to using other terms such as "attributes" or "felds", but
for the purposes of this reference we will use "properties".

Theyare defned by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration
may include an initiali9ation, but this initiali9ation must be a constant value..that is, it must be able to be evaluated at compile time and
must not depend on run.time information in order to be evaluated.
Constructors : ;estructors
Constructor 5.
<<construct!# function is used to defne a predefned Constructor in PHP.
PHPallows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each
newly.created ob2ect, so it is suitable for any initiali9ation that the ob2ect may need before it is used.
;estructor 5.
<<destruct!# function is used to defne a destructor in PHP.
PHPintroduces a destructor concept similar to that of other ob2ect.oriented languages, such as C==. The destructor method will be called as
soon as there are no other references to a particular ob2ect, or in any order during the shutdown se4uence.
>b2ect Cloning
Creating a copy of an ob2ect with fully replicated properties is not always the wanted behavior. ?3ample5 is if your
ob2ect holds a reference to another ob2ect which it uses and when you replicate the parent ob2ect you want to create
a new instance of this other ob2ect so that the replica has its own separate copy.
n ob2ect copy is created by using the clone keyword !which calls the ob2ect+s <<clone!# method if possible#. n
ob2ect+s <<clone!# method cannot be called directly.
)copy<of<ob2ect* clone )ob2ect,
Example #1 Cloning an object
$%php
class &ub>b2ect
'
static )instances * @,
public )instance,
public function <<construct!# '
)this./instance * ==self55)instances,
0
public function <<clone!# '
)this./instance * ==self55)instances,
0
0
class AyCloneable
'
public )ob2ectB,
public )ob2ectC,
function <<clone!#
'
(( 7orce a copy of this./ob2ect, otherwise
(( it will point to same ob2ect.
)this./ob2ectB * clone )this./ob2ectB,
0
0
)ob2 * new AyCloneable!#,
)ob2./ob2ectB * new &ub>b2ect!#,
)ob2./ob2ectC * new &ub>b2ect!#,
)ob2C * clone )ob2,
print!">riginal >b2ect5Dn"#,
print<r!)ob2#,
print!"Cloned >b2ect5Dn"#,
print<r!)ob2C#,
%/
http5((www.ape3tgi.in
Thank 8ou
Apex TG India
E-20 , Sector 63, Noida
0120 402000!024!02"!02#
$1-"3"%4"4%
E&ai' id( pratap)apext*i+co&
&tay Connected with us for morechaptersonPHP

S-ar putea să vă placă și