class cow GwU nQ GKwU cow Kvm { GwU nQ Kvm Gi ewW A_vr GLvb AeR mK me wKQz GLvb ejv _vKe| } wb K vm Gi GKwU D`vniY `Iqv nj <?php class opc { public $name="Kamal Ahmed";
fjy GwU nQ fwiqej }
K vm wbR wbR KvR Ki bv hw` bv ZvK Kj (call) Kiv bv nq| cvM vg ivb Kie wK K vm Gi wfZi h g_W Ges cvcvwUR AvQ mjv KvR Kie bv| ZvB GLb `Lev wKfve K vm K ivb (execute) KiZ nq| K vm K ivb KiZ nj c_g K vmi AeR Zwi (create) KiZ ne| $ob=new opc; A_vr c_g h Kvb GKwU fwiqej wbZ ne hgb GLvb $ob wbqwQ| Avi $ob B nQ opc K vm Gi AeR| Zvici BKzqvj (=) mvBb wjL wbqg Abyhvqx new Kx-IqvWwU wjLZ ne Gi ci m (space) w`q K vm Gi bvg wjL mwgKvjb eenvi KiZ ne| gb ivLeb mvaviYZ K vm Ges AeR Gi bvg h Kvb wKQz nZ cvi|
Pjyb GLb `wL H ob AeRwU wK KvR Ki|
9
c_g Avgiv ob AeR w`q opc K vm Gi fwiqej $name Gi gvb (value) `LZ PvwQ| echo $ob->name; GLb Avgiv AvDUcyU `Lev Kamal Ahmed AekB gb ivLeb GB eBwU covi c~e Avgv`i jLv wcGBPwc Av gvBGmwKDj eBwU AekB coeb| Ab_vq GB eBwU eySZ Avcbvi mgmv nZ cvi| <?php include('p1.php');
class MyClass { public $pop = "I'm a class property!"; // pop variable in the calss }
$obj = new MyClass; // the way to create object
echo $obj->pop; // Output the property (want to see the output of class)
GLb `Lev AeR Avgiv Kb cvM vwgs G eenvi Kwi| Avgiv hLb Kvb K vm jwL H K vm AbK fwiqej, dvskb BZvw` _vK Avi mjv wbR wbR KvR Ki bv hZY bv ch AeR viv H wjK Avgiv Kj (WvKe) bv Kwi | K vmi fwiqejK H K vmi AeR viv KvR KiZ ne| AeR Zwi KiZ nq Kvb GKwU K vmi Rb| hgb eB hw` GKwU K vm nq Zvnj GKwU AeR ejv hZ cvi BwZnvm| GLvb BwZnvm nQ eyK K vmi AeR| wcq cvVK GLvb Avwg jLvjvK ^vfvweK fvlvq jLvi Pv KiwQ m Rb nqZv AbK fvjv gvbi evsjv evKiY mshy jLv nq bvB, mRb `ytwLZ|
wbb KZjv AeR Gi Qwe `Iqv nj|
wP 4: cb wP 5: M vm wP 6: eB wP 7: Pqvi
AeR Zwi (create) KiZ nj Kvb bv Kvb GKwU K vm _vKZ ne| GLb Avwg Avcbv`iK gvb (man) K vm Gi GKwU AeR Zwi Ki `Lvev| AeR Zwi (create) KiZ nj c_g Wjvi mvBb ($) w`q h Kvb GKwU bvg w`Z ne hwU nQ AeR bBg Zvici BKzqvj (=) mvBb eenvi KiZ ne Gici wbD (new) bvgK Kx-IqvWwU eenvi KiZ ne Zvici K vmi bvgwU (h K vmi Rb AeRwU) wjLZ ne Gici `yUv c_g evKU () w`q Zvici mwgKvjb (;) w`q kl KiZ ne| $o=new man(); Dcii $o nQ man K vm Gi AeR 6: AeR Zwi:
11
$object=new man(); $m=new man(); $Rakib=new man(); Dci $object, $m, $Rakib nQ man K vm Gi AeR | Avcwb AeR Gi bvg h Kvb wKQz w`Z cvieb| <?php
class human { // Name of class public $gender='Male'; // in the gender variable assign male value } $manik = new human(); // create object manik echo $manik->gender; // access the varible gender using humnan class object //manik
?>
c vM vg p4.php: K vm Ges AeR| AvDUcyU: Male gb ivLeb evev Qvov hgb mvb nZ cvi bv, wVK Zgwb K vm Qvov AeR nZ cvi bv| AbK mgq Qji bvg ivLvi mgq evevi bvgi mv_ wgj iL ivL| wK cvM vwgs G AeR Zwi Kivi mgq AekB K vm Gi bvg ZLb _vK| mvb Gi bvg hgb h Kvb wKQz nq AeR Gi bvgI Avcwb h Kvb wKQz w`Z cvib| B-Kgvm mK wevwiZ RvbZ Avgv`i jLv B-Kgvm eBwU coZ cvib| eBwUZ B-Kgvm mK wevwiZ ejv AvQ|
<?php
class Kool { public $mvar = 'aMemberVar Member Variable'; public $aFuncName = 'Function of member';
function mfunc() { print 'Mfunc is the insde of kool class'; } }
12
$kk = new kool; echo $kk->mfunc(); ?> c vM vg p5.php: K vm Ges AeR| AvDUcyU: Mfunc is the insde of kool class
g_W nQ GK aiYi dvskb| wbw` wKQz KvR g_W Ki _vK| A_vr g_W me mgq GKB aiYi KvR Ki _vK hvi Rb GKB aiYi Kvb KvR Kivi Rb hb GKB KvW evi evi wjLZ bv nq| m Rb Avgiv g_W eeni KiZ cvwi| gb ivLeb Avgiv Pv KiwQ jLvjvK cPwjZ k wjLZ m Rb nqZv mwVK k eenvi nQ bv m Rb Avgiv `ytwLZ| g_WK mvaviYZ Avgiv K vmi wfZi wjL _vwK| Avcwb PvBj Kvb Z_ K vm Gi evBi _K K vmi g_W Gi wfZi cvVvZ cvib| Ze gb ivLZ ne g_W A_ev dvskb wbR wbR ivb (execute) Ki bv hZY bv ch GwUK Kj (call) Kiv bv nq| g_W AeR Kvb GKwU wbw` KvR KiZ mvnvh Ki| D`vniY wnme ejv hvq gvDm wK K AbKUv g_W Gi gZ| hgb Avgiv hLb dvvi Icb Kwi gvDm wK K Kwi, hLb Kvb dvBj Icb Kwi gvDm wK K Kwi, hLb Kvb evDRvi Icb Kwi gvDm w`q wK K Kwi A_vr GLvb me KvRi Rb gvDm wK K GKwU Kgb KvR|
wP 8: ebWvi gwkb wP 9: M vm GKB gwkb eenvi Ki Avcwb AbK aibi Rym Zwi KiZ cvib wVK Zgwb GKB M vm eenvi Ki AbK wKQz cvb KiZ cvib| Ggwb AbK GKB M vm eenvi KiZ cvib| 7: Kvm g_W:
AvMB ejwQ g_W wWK vikb nQ AbKUv dvskb Gi gZ| c_g h Kvb GKwU K vm wWKqvi KiZ ne Zvici K vm Gi wfZi g_WwU wjLZ ne| c_g dvskb (function) bvgK Kx- IqvWwU wjLZ ne Zvici g_W Gi Rb GKwU bvg wjLZ ne, Gici `ywU c_g evKU () w`Z ne, Gici GKwU mK evKU ({) Gici g_W Gi wfZi hv wjLZ Pvb wjLvi ci AviKwU mK (}) evKU w`q kl KiZ ne| c vM vg wmbUvK 3 : dvskb (g_W)
dvskb Kx-IqvW g_W bBg
function Room() evKU { mK evKU i/g_W i function glass {
} 8: g_W Zwi (Method Declaration):
14
g_W ewW echo "this is class room"; } g_W kl
c vM vg p6: g_W wWK vikb <?php
class university // class name university { function Room() // method name Room // { //Start method echo "This is class room"; // method statement // Execute only if call this method outside of this class } //End method
}
$obj = new university(); // after class you can use first bracket or not use
echo $obj->Room(); // Call method, method will not execute if you dont call //method
?> c vM vg p6.php:g_W wWK vikb AvDUcyU: This is class room
<?php
class university // class name university { function course() // method name Room // { //Start method echo "Software Engineering "; // method statement // Execute only if call this method outside of this class } //End method
}
$obj = new university(); // after class you can use first bracket or not use
15
echo $obj->course(); // Call method, method will not execute if you dont call //method
?>
c vM vg p7.php: g_W Kwjs AvDUcyU: Software Engineering
<?php
class university { public $name = 'John'; // declare varibale name and value is john
function getName() // method name getName // function is keyword { //return $this->name; echo $this->name; // when call getName method it will display John
}
} $obj =new university(); $obj->getName();
?> c vM vg p8.php: g_W Ges fwiqej AvDUcyU: John
<?php
class university { public $name = 'Mohsin'; // declare varibale name and value is john
function getName() // method name getName // function is keyword { //return $this->name; echo $this->name; // when call getName method it will display John
}
}
16
$obj =new university(); $obj->getName();
?> c vM vg p9.php: K vm fwiqej Ges g_W AvDUcyU: Mohsin
Avcwb hw` g_W Kvb fvjy cvVvZ Pvb Zvnj wK fve cvVveb| mRb g_W Gi evKUi () Gi wfZi fwiqej wjLZ ne| Avi g_W Kj Kivi mgq fvjywU w`q w`Z ne| penType($price) GwU nQ cvivwgUvi $ob->penType(2) cvivwgUvi fvjy <?php
class pen {
function PenType($price) // $price is parameter its vlaue come when call it { echo $price; // display the value of $price }
} $obj =new pen(); $obj->PenType(2); // passing value of $price, value is 2
?> c vM vg p10.php: g_W cvivwgUvi AvDUcyU: 2 <?php
class pen {
9: g_W wiUvb Ges cvivwgUvi (Method Return and Parameter)
17
function PenType($price, $penName) // if more than one parameter write using comma { echo $price; // display the value of $price echo $penName; }
} $obj =new pen(); $obj->PenType(13, "Econo"); // if want to pass more paramter value write using comma
?> cvMvg p11.php: GKi AwaK cvivwgUvi AvDUcyU: 13Econo <?php
class pen {
function PenType($price, $penName, $companyName) // if more than one parameter write using comma { echo $price; // display the value of $price echo $penName; echo $companyName; }
} $obj =new pen(); $obj->PenType(13, "Econo", "Ocan"); // if want to pass more paramter value write using comma
?> c vM vg p12.php:Parameter cvVvbv AvDUcyU: 13EconoOcan g_W _K Avgiv wKfve Kvb iRv cZ cvwi| A_vr g_W Gi Kvb fvjy Avgiv wbZ PvB hUv ej g_W wiUvb| A_vr g_W Kvb GKwU KvR Ki Zvi fvjyUv hw` K vm A_ev g_W Gi evBi cvVvq mUvB nj g_W wiUvb fvjy| g_W fvjy cvVvbvi (Return) Kivi Rb Return Kx-IqvWwU eenvi Ki| A_vr Return 5; GLvb 5 K cvVvQ hLvb _K Kj Kiv nQ mLvb|
<?php
18
class pen {
function PenType() { return 5; // return value 5 // use return keyword to reutrn vlaue }
} $obj =new pen(); echo $obj->PenType(); // Here we will get 5 becuase call PenType from here
?> c vM vg p13.php: wiUvb g_W fvjy AvDUcyU: 5
<?php
class pen {
function PenType() { return 45.23; // return value 5 // use return keyword to reutrn vlaue }
} $obj =new pen(); echo $obj->PenType(); // Here we will get 5 becuase call PenType from here
?> c vM vg p14.php:g_W wiUvb fvjy AvDUcyU: 45.23
<?php
class pen {
function PenType() { return "Econo"; // return string type value like Econo }
19
} $obj =new pen(); echo $obj->PenType(); // Here we will get Econo
?> c vM vg p15.php: ws wiUvb AvDUcyU: Econo
<?php
class pen { public $price=50; function PenType() { return $this->price; // return string type value like Econo }
} $obj =new pen(); echo $obj->PenType(); // Here we will get Econo
class human { // { this is starting bracket 1 for this class
function __construct() // {
echo "This is Bangladesh"; // statement inside the constructor
}
} // { this is ending bracket 2 for this class human
10: KbvKUi (Constructor)
22
$akash = new human(); // call consturctor automatically when create object // Do not need to call seperately constructor
?> c vM vg p17.php:ewmK Constructor AvDUcyU: This is Bangladesh
<?php
class human { // { this is starting bracket 1 for this class
function __construct() // {
echo "This is Bangladesh"; // statement inside the constructor
}
} // { this is ending bracket 2 for this class human
$akash = new human(); // call consturctor automatically when create object // Do not need to call seperately constructor
?> c vM vg p17.php:KbvKUi cvM vg AvDUcyU: This is Bangladesh
<?php
class human { // { this is starting bracket 1 for this class public $p=0;
function __construct() { $p=15; echo $p;
}
23
} // { this is ending bracket 2 for this class human
$akash = new human(); // call consturctor automatically when create object // Do not need to call seperately constructor
?> c vM vg p18.php:KbvKUi D`vniY AvDUcyU: 15
Constructor wK fvjy M nYI Ki cvivwgUvi wnme| hgbwU g_W fvjy M nY Ki| Ze Constructor G fvjy cvVvZ ne AeR Create Kivi mgq| function _Consturct($Name) parameter 1wU {
} function _Construct($Name,$age) parameter 2wU
gb ivLeb Constructor G GKi AwaK cvivwgUvi _vKZ cvi|
<?php
class human { // { this is starting bracket 1 for this class
function __construct($name) {
echo $name;
11: KbvKUi G fvjy cvVvbv:
24
}
} // { this is ending bracket 2 for this class human
$akash = new human('Jebin');// call consturctor automatically pass value jebin // Do not need to call seperately constructor
?> c vM vg p19.php:GKwU fvjy cvVvbv AvDUcyU: Jebin
<?php
class human { // { this is starting bracket 1 for this class
function __construct($name, $age) // name and age two parameter value come from when create object {
echo $name; echo $age;
}
} // { this is ending bracket 2 for this class human
$akash = new human('Jebin', 21);// call consturctor automatically pass value jebin // passing two vlaue jebing and 21 during object creation
?> c vM vg p20.php:KbvKUi GKi AwaK fvjy AvDUcyU: Jebin21
25
Avgiv hLb Kvb AeRK destroy (asm) KiZ PvB ZLb WvKUi eenvi Ki _vwK| A_vr Kvb K vm Gi Zwi (created)AeR hw` Avcwb kl Ki w`Z Pvb A_vr fwelr G GwU Avcbvi Avi KLbv cqvRb coebv ZLb Avcwb Destructor eenvi Ki AeRwUK destroy Ki w`Z cvib| wcGBPwcZ GwUK Garbage Collector w`q Kiv nq| WvKUiI GK aiYi g_W| Constructor Gi KvR nQ AeR Zwi Kiv Avi Destructor Gi KvR nQ mB AeRK kl Ki `Iqv| AeR hLb destroy nq destructor ZLb AUvgwUKvjx Kj nq|
<?php
class X { public function __construct() // This is constructor { echo 'building an object'; }
public function __destruct() // destructor to destroy obejct { echo 'destroying the object'; } }
$obj = new X(); // when create object both construct and destruct call automatically
?> c vM vg p21.php:AeR Ges destroy AvDUcyU: building an objectdestroying the object
<?php
class Yea { public function __destruct() { echo 'destroy the object'; } 12: WvKUi (Destructor):
26
}
$obj = new Yea();
// prints Bangladesh echo 'Banglade4sh ';
?> c vM vg p22.php:destroy AeR AvDUcyU: Banglade4sh destroy the object gb ivLZ ne mKj KvR kl Kivi ci AeR destroy KiZ ne A_vr destruct g_WwU wjLZ ne K vmi kl hb Zvi AvM Abvb KvRjv kl nq hvq|
<?php class A { public function printMsg() { echo 'I still exist'; }
public function __destruct() { echo 'destroying the object'; }
}
$b = new A();
$d = $c = $b;
$b = null;
$c->printMsg(); $d->printMsg();
?> c vM vg p23.php:K vm kl destruct g_W AvDUcyU: I still existI still existdestroying the object
wekl aiYi wKQz Kx IqvW Avgiv eenvi Ki _vwK K vm, g_W, dvskb Ges fvwiqej Gi c~e Avi GwUKB ej modifier, AvKmmK KbUj Kivi Rb Avgiv gwWdvqvi eenvi Ki _vwK| gwWdvqvi K Avevi visibility I ejv nq| modifier jv nQ hgb public, private, protected, Final Ges Abstratct
cvewjK (Public): Kvb K vm, fwiqej Ges dvskb Gi c~e cvewjK Kx-IqvWwU _vKj GwUK Avcbvi w Gi h Kvb RvqMvq eenvi Kiv hve| gb ivLeb H Kvm A_ev cvcvwUjv Avcbvi wcGBPwc dvBj me RvqMvq eenvi Kiv hve| hw` Kvb K vmi wfZi g_W A_ev fwiqej cvewjK, mjvK K vmi wfZi GgbwK evBiI eenvi Kiv hve| <?php
class test { public $abc='I am defined public'; // abc variblae defined by public
}
$objA = new test(); echo $objA->abc;//accessible from outside because abc defined by public
?> c vM vg p24.php: cvewjK fwiqej AvDUcyU: I am defined public
<?php
class test { public function xyz() // xyz mehtod defined by public, xyz accessible from outside class { echo 'I am defined public method'; }
}
13: gwWdvqvi (Modifiers):
29
$objA = new test();
$objA->xyz();//public method of the class test
?> c vM vg p25.php:cvewjK g_W AvDUcyU: I am defined public method
<?php
class test { public $abc='you can access me (abc) outside of test class '; public $xyz; public function xyz() { echo 'you can access me (zyz) outside of test class '; } } $objA = new test(); echo $objA->abc;//accessible from outside $objA->xyz();//public method of the class test
?> c vM vg p26.php:cvewjK fwiqej Ges g_W AvDUcyU: you can access me (abc) outside of test class you can access me (zyz) outside of test class cvBfU (Private): cvBfU g_W Ges cvcvwU mg~n ay gv K vmi wfZi eenvi Kivi Rb| cvBfU fvwiqej Ges dvskbK K vmi evBi eenvi Kiv hve bv| Ze K vmi gvS eenvi Kiv hve $this Kx-IqvW eenvi Ki| ck nj Kb dvskb A_ev fvwiqejK cvBfU Kiev A_vr Avcwb hw` KLbv gb Kib Avcbvi K vmi cvcvwU K vmi evBii KD eenvi bv KiK ZLb cvBfU eenvi KiZ ne| <?php
class test
30
{ private $abc='you can access me (abc) inside of of test class ';
public function xyz() { echo $this->abc; }
} $objA = new test(); //echo $objA->abc;//accessible not possible from outside the test class $objA->xyz();//public method of the class test
?> c vM vg p27.php:cvBfU fvwiqej AvDUcyU: you can access me (abc) inside of of test class
<?php
class Car { private $NumberOfDoors;
public function __construct() { $this->NumberOfDoors = 4; echo $this->NumberOfDoors; }
}
// instantiate $car= new Car(); // show the property - not allowed
?> c vM vg p28.php:cvBfU fvwiqej Ges Constructor AvDUcyU: 4 cvUKUW (Protected): c vUKUW nQ AbKUv cvewjK Ges cvBfU Gi gvSvgvwS| Ze cvUKUW nQ cvBfU Gi gZ Ze mwUK wcGBPwc K vm eenvi Kiv hve BbnwiU Ges BUvidm Gi | wcq cvVK, Avgiv Dcvcb Ges eySvbvi
class pc // pc is parent class { protected $pr=451; // define protected to stop access outside the class // but the childe class can access the pr varible }
class cc extends pc // cc is child class is so it can access the pr { public function testChild() { echo $this->pr; //will work because it cc is child of pc }
}
$objChild = new cc(); $objChild->testChild();//work because test child will call
?> c vM vg p30.php:cvUKUW fvwiqej GK mm AvDUcyU: 451 <?php
class parent { protected $pr; public $a protected function testParent() { echo this is test; } } class child extends parent { public function testChild() { $this->testParent(); //will work because it }
32
} $objParent = new parent(); $objParent->testParent();//Throw error $objChild = new Child(); $objChild->setChild();//work because test child will call test parent.
?>
c vM vg p31.php:gwWdvqvi Gi eenvi
<?php
class Myclass
{
public $title;
public function DispTitle()
{
echo $this -> title;
echo "<br />";
}
}
$obj = new Myclass();
$obj->title = "ABCXYZ";
echo $obj->title;
echo "<br />";
echo $obj->DispTitle();
?>
33
c vM vg p32.php:gwWdvqvi Gi eenvi AvDUcyU: ABCXYZ ABCXYZ
<?php class Myclass { protected $title=10; } class Baseclass extends Myclass { function __construct() { echo $this->title; } } $obj = new Baseclass(); ?> c vM vg p33.php:gwWdvqvi Gi eenvi AvDUcyU: 10
<?php class myClass { public $public = 'Public'; protected $protected = 'Protected'; private $private = 'Private';
$obj2 = new myClass2(); echo $obj->public; // Works echo $obj2->private; // Undefined echo $obj2->protected; // Fatal Error
// Shows Public, Protected2, not Private $obj2->printHello();
?> c vM vg p34.php: wewfb gwWdvqvi mg~n AvDUcyU: Public
wbR K vmi Kvb cvcvwU hgb fvwiqej Ges g_W eenvi Gi Rb this Kx-IqvW cqvRb nq| Avevi this Kx-IqvW eenvi Ki AeR viv H K vmi property jv AvKmm Kiv hvq|
<?php
14: this Kx IqvW:
35
class Foo { public $Bar; public function __construct() { $this->Bar = 0; // use this to access Bar varibale } public function Baz() { $this->Bar = 10; echo $this->Bar; } } $f1 = new Foo(); $f2 = new Foo(); $foo1->Baz(); // sets the value of $Bar inside $foo1 to 10 // does not affect the $Bar inside $foo2
?> c vM vg p36.php: this Kx-IqvW Gi eenvi AvDUcyU:
GKwU Qj Zvi evevi m` cve GUvB ^vfvweK A_vr evevi m` mvaviYZ Qj webv AbygwZZB eenvi KiZ cvi , GUvB nQ BbnwiU| A_vr Avcbvi evevi GKwU Mvwo AvQ GwU Avcbvi evev eenvi KiQ Avevi gvS gvS Avcwb eenvi KiQb wK hw` MvwowU Avcbvi PvPvi nZv nqZv ev Avcwb PvBjB eenvi KiZ cviZb bv, GUvB nQ Inheritance Gi gRvi welq| GLb `wL Avmj cvM vwgs G GwU Kgb| cvM vwgs G GKwU K vm Ab GKwU K vm Gi fvwiqej A_ev g_W eenvi KiZ cvie hw` BbnwiU nq| A_vr PvB K vm cviU (parent) K vm Gi cvcvwU eenvi KiZ cvie|
B-Kgvm IqemvBU Zwi Kivi mnR Kkj RvbZ Avgv`i jLv B-Kgvm G Rygjv! fvPzgvU eBwU coZ cvib|
15: BbnwiU (Inheritance):
36
GKB KvW evi evi eenvi Kiv hvq| KvW Gi mvBR AbK Kg hvq| GKB aiYi KvRi Rb GKB aiYi KvW evi evi wjLZ nq bv| GKB K vmi cvcvwU AbKjv K vm eenvi KiZ cvi| parent K vm PvBj wKQz cvcvwU hide Ki ivLZ cvi A_vr PvB Kvm mjv GvKmm (eenvi) KiZ cviebv| eBm (parent)K vmK GK U Kiv hvq| c vM vg Gi ivb UvBg Kg hvq| parent K vm cwieZb Kij PvB K vmI cwieZb nq|
BbnwiU ZLbB nq hLb GKi AwaK K vm _vK| hgb wbb `ywU K vm GKwU nQ p K vm hwUK Avgiv cviU K vm, Ab GKwU K vm c hwUK Avgiv PvB K vm ejwQ| class p class p GwUK mycvi/eBm/cviU Kvm I ejv nq {
} class c GwUK PvB/GKU Kvm I ejv nq
class c extends p { GB Kx-IqvWwU Inheritance Kivi Rb eenvi KiZ nq
16: BbnwiU Gi myweav:
17: BbnwiU cvMvg:
37
}
c vM vg wmbUvK 5 : BbnwiU cvM vg wmbUvK BbnwiU Kivi wbqg nQ: PvB Kvm wjL extends Kx-IqvW wjL mycvi K vmwU wjLZ ne|
gb ivLeb cviU K vm K eBm A_ev mycvi K vmI ejv nq|
<?php
class P { public $n=12; // n declared in p class and assign value 12 }
class C extends P { function display() { echo $this->n; // print n in the c lass but c is varible in p class // since c is the child of p for that reason n is printed in the display method } }
$io=new C(); $io->display();
?> c vM vg p37.php: K vm Ges BbnwiU AvDUcyU: 12
<?php
class class_A{
38
public function hello() // hello function in the super class { return "Hello I am parent"; // hello method return Hello I am parent } }
class my_class extends class_A {
public function greeting() { echo $this->hello(); // call hello mehtod of Class_A and hello method reuturn 'Hello I am parent' } }
$obj = new my_class; // object of base class my_class echo $obj->greeting(); // call greeting method of base class
?> c vM vg p38.php: g_W eenvi Ges BbnwiU AvDUcyU: Hello I am parent
<?php
class class_A{
public function hello($name) // hello function in the super class { return $name; // hello method return Isita } }
class my_class extends class_A {
public function greeting($name) { echo $this->hello($name); // call hello mehtod of Class_A } }
$obj = new my_class; // object of base class my_class
39
echo $obj->greeting('Isita'); // call greeting method of base class
?> c vM vg p39.php: BbnwiU Ges fvjy cvwms AvDUcyU: Isita
<?php class kok { var $_name;
function Pet($name) { $this->_name = $name; }
function eat() { }
function sleep() { echo 'I am sleeping'; } }
class Dog extends kok { function bark() { } }
class Cat extends kok { function meow() { } }
$dog = new Dog("Max"); $dog->eat(); $dog->bark(); $dog->sleep();
40
$cat = new Cat("Misty"); $cat->eat(); $cat->meow(); $cat->sleep(); ?> c vM vg p40.php: GKi AwaK PvB K vm BbnwiU AvDUcyU: I am sleepingI am sleeping
<?php
class A { // code goes here }
class B extends A { // code goes here }
class C extends B { // code goes here }
$oA = new A(); $oB = new B(); $oC = new C();
?> c vM vg p41.php: BbnwiUv wmbUvK
KviU Gi myBP Avgiv mevB eenvi Kwi| GKB myBP Avcwb wewfb KvR eenvi KiZ cvib hgb dvb, jvBb Ab-Ad Kivi Rb Avi GwUB nQ cwjgiwdRg| poly A_ nQ AbK (many) Avi morph A_ nQ dg (forms) Zvnj ejv hvq polymorphism A_ nQ AbK dg| A_vr BjKwUK myBP jv GKB njI GKKwU Avjv`v Avjv`v KvRi Rb hgb GKwU myBPi KvR nj jvBU Ab Kivi Rb Ab myBPi KvR nj dvb Ab Kivi Rb|
18: cwjgiwdRg (Polymorphism):
41
c vM vwgs G cwjgiwdRg eySv AbK mnR hgb Avcbvi `yUv g_W GKB bvg AvQ wK `yUv g_W `yUv wewfb KvR Ki GwUI nQ cwjgiwdRg| Abfve ejv hvq GKB dvskb hLb wewfb Dk eenvi Kiv nq|
Kvb Kvm Gi g_W Ges fvwiqejK H K vm Qvov Ab K vm eenvi KiZ cvie wK cvie bv| Encaptulation nQ Ggb GKwU cwZ hwU K vm Gi WUv Ges dvskb GK mv_ Ki ivL K vm Gi wfZi| GwU WUvK nvBW (hide) Ki ivL, hb K vm evBi _K mivmwi (Directly) eenvi KiZ bv cvi| wK K vm Gi dvskbK eenvi Ki, PvBj K vm evBi _K K vm Gi Z_ eenvi KiZ cvi|
<?php
class enc { private $p=10; // this is private varibale usually it is not possible to access outside the class function mn() { return $this->p; // since p is not possible use outside the class } // for this reason return it from where call the mn method
}
21: cvMvwgs G qbKcmyjkb (Encaptulation):
22: qbKvcmykb cvMvwgs:
20: qbKcmyjkb (Encaptulation):
45
$obj=new enc(); // object of enc class // using this object we cant call p varible which is in enc class // if we write echo $obj->p; it will not work because p is private echo $obj->mn(); // if we call mn method we get p
AvemUvKU K vmK Ab Kvb K vm BbniU Ki| K vm Ges g_W `yUvB Abstract nZ cvi Ze g_W Abstract nj K vm AekB Abstract nZ ne| gb ivLZ ne Kvb Abstract K vm Gi g_W Abstract nj mB g_W Gi BgwcgbUkb (implementation) A_vr ewWZ wKQz _vKe bv| Abstract K vm Gi Kvb AeR create (Zwi) Kiv hvq bv|
B-Kgvm IqemvBU Zwi Kivi mnR Kkj RvbZ Avgv`i jLv B-Kgvm G Rygjv! fvPgvU eBwU coZ cvib|
h K vm Abstract K mK BbniU Kie mB K vm g_W jvi ewW (implementation) wWdvBb Kiv _vKZ n
23: AvemUvKU (Abstract):
24: cvMvwgs AvemUvKU:
25: AvevKU Kvm wmbUvK&:
47
wb GKwU AvevKU K vm Gi wbqg `Lvbv nj abstract Kx-IqvW
abstract class nima { }
class Kx-IqvW Kvm bBg
c vM vg wmbUvK 5 :AvevKU K vm cvM vg wmbUvK AvevKU K vm Gi c~e AekB abstract Kx-IqvWwU wjLZ ne| Zvici class Kx-IqvWwU Gi ci K vm Gi bvg
<?php abstract class nima abstract public function myni(); ?
<?php
abstract class Foo { // Foo is abstract class abstract public function foobar(); // foobar is abstract method no body of this method }
class Baz extends Foo { dvskb Kx-IqvW
abstract public function myni( ); mwgKvjb
gwWdvqvi c_g ebx
g_W bBg
26: GvemUvKU cvMvwgs:
48
public function foobar() { // implemetn the foobar method in the Baz class echo "My name is Rashid"; // only child class can implement the abstract method } } // we cant create the object of Foo class like $ob=new Foo(), not possible because abstract class
$h=new Baz(); $h->foobar();
?> c vM vg p46.php: ewmK GvemUvKU cvM vg AvDUcyU: My name is Rashid
<?php
abstract class A{
abstract function disp();
}
class B extends A{
public function disp(){
echo "Inside the child class<br/>";
}
}
class C extends A{
public function disp(){
echo "Inside the child class 2<br/>";}
}
$two=new B();
echo "<b>Calling from the child class B:</b><br/>";
49
$two->disp();
echo "<b>Calling from the child class C:</b><br/>";
$three=new C();
$three->disp();
?>
c vM vg p47.php: GvemUvKU AvDUcyU: Calling from the child class B: Inside the child class Calling from the child class C: Inside the child class 2
BUvidBm Kvmi Kvb g_W Gi Wwdbkb _vK bv| Interface K vm ay g_W Gi Defination _vK| BUvidBm K vm Gi g_W K hw` Ab K vm eenvi (Implement) KiZ Pvq, Ze gb ivLZ ne H K vmK AekB BUvidBm K vmi mKj g_WK evevqb KiZ ne|
wP 11: wigvU Ges Uwjwfkb Dcii wPwU j Kib wigvUwU wUwf Ges `kK Gi ga BUvidBm wnmve KvR KiQ|
public function xyz() { xyz Kvm xyz g_W Gi ewW } }
c vM vg wmbUvK 8 : BUvidBm K vm wmbUvK
BUvidBm K vmK h Kvm eenvi Kie mB K vm AekB Interface K vmK GK U KiZ ne| BUvidBm K vmK GKU KiZ nj AekB implements Kx-IqvWwU eenvi KiZ ne| BUvidBm K vmi g_W AekB cvewjK ne Ges fvwiqej KbvU ne|