Monday, August 27, 2012

Basic class ေရးနည္း

Classes and Objects
Object ပံုစံဆိုတာ လုပ္ေဆာင္ခ်က္ေတြ ပိုေကာင္းေစဖို႔ ထပ္ခါထပ္ခါ ျပန္ေရးနိုင္တာကိုခြင္႔ျပဳေပးတာပါျပီးေတာ႔
feature ေတြအမ်ားၾကီးလည္းပါ၀င္ပါတယ္ဒါက php 4 ကေန 5 ကိုေျပာင္းသြားရတဲ႔ အဓိကအေၾကာင္းရာပါပဲ႔
php 5 မွာ object model ေတြအျပည္႔စံုပါ၀င္ပါတယ္

ဟုတ္ျပီ ကၽြန္ေတာ္က feature ေတြပါ၀င္တယ္လို႔ေျပာခဲ႔တယ္  PHP 5 မွာဘယ္လို  feature ေတြပါလဲဆိုေတာ႔
visibility , abastract and final ဆိုတဲ႔ class သံုးခုနဲ႔ method ေတြပါ၀င္ပါတယ္ျပီးေတာ႔
magic methods , interfaces ,cloning and typehinting စတာေတြကိုျဖည္႔စြက္ထားပါေသးတယ္

Basic Class
Basic class ကိုေတာ႔ class keyword နဲ႔စတင္ျပီး သတ္မွတ္ပါတယ္သူ႔ေနာက္မွာေတာ႔ class name ထည္႔ရပါတယ္
class name ရဲ႕ ေနာက္မွာေတာ႔ curly braces { } ထည္႔ေပးရမွာပါ
curly braces ထဲမွာေတာ႔ properties and method ကိုထည္႔ေရးနိုင္မွာတယ္  အဲဒီ curly braces ထဲမွာေရးထားတဲ႔
properties and method ကိုေတာ႔ သူရဲ႕မူရင္း Base Class က ပိုင္ဆိုင္ပါတယ္္
Class name ကိုေတာ႔ ၾကိဳက္တာေပးလို႔ရပါတယ္ ဒါေပမယ္႔ PHP ရဲ႕reserved word ကိုေတာ႔သံုးလို႔မရပါဘူး
reserved word ကိုေတာ႔ေနာက္မွရွင္းျပေပးပါမယ္ သံုးနိုင္တဲ႔ class name ကိုေတာ႔ letter သို႔ undersort နဲ႔ စတင္နိုင္ပါတယ္
သူေနာက္ကေတာ႔ၾကိဳက္တဲ႔ number or letter ေတြေရးနိုင္ပါတယ္ underscores ေတြလည္းေရးနိုင္ပါတယ္
regular  expression လိုမ်ိဳးပါေဖာ္ျပေပးလိုက္ပါတယ္
[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*.

Class တစ္ခုမွာ properties လို႔ေခၚတဲ႔ကိုယ္ပိုင္ constants , variable ေတြနဲ႔ methods လို႔ေခၚတဲ႔ functions ေတြ ပါ၀င္ပါတယ္
ဟုတ္ျပီ ဒါဆို ကၽြန္ေတာ္တို႔ basic class ေလးတစ္ခုေရးလိုက္ၾကရေအာင္
<?php
class SimpleClass
{
    //property ကိုေၾကျငာျခင္း
    public $var = " I live in class ";
   
    //method ကိုေၾကျငာျခင္း
    public function display()
    {
        echo $this->var;
    }
}
?>

The Output is
 I live in class

 $this ဆိုတာက pseudo-variable ျဖစ္ပါတယ္ object ထဲမွာပါတဲ႔  အေၾကာင္းရာေတြကိုျပန္ေခၚသံုးခ်င္တယ္ဆိုရင္သံုးရပါတယ္
 $this က ျပန္ေခၚသံုးတဲ႔ object ကိုရည္ညြန္းတာပါ

 $this pseudo-variable နမူနာ
<?php
class A
{
    function foo()
    {
        if (isset($this)) {
            echo '$this is defined (';
            echo get_class($this);
            echo ")\n";
        } else {
            echo "\$this is not defined.\n";
        }
    }
}

class B
{
    function bar()
    {
        // Note: the next line will issue a warning if E_STRICT is enabled.
        A::foo();
    }
}

$a = new A();
$a->foo();

// Note: the next line will issue a warning if E_STRICT is enabled.
A::foo();
$b = new B();
$b->bar();

// Note: the next line will issue a warning if E_STRICT is enabled.
B::bar();
?>

The Output is
$this is defined (A)
$this is not defined.
$this is defined (B)
$this is not defined.

new
class တစ္ခုရဲ႕  ျဖစ္စဥ္ကိုဖန္တီးဖို႔အတြက္ new keyword ကိုအသံုးျပဳရပါမယ္
Object မွာရွိတဲ႔ constructor ကို define မလုပ္ပဲ႔ဲ႔ object ကိုဖန္တီးနိုင္ပါတယ္
Class ေတြကိုေတာ႔ instantiation မလုပ္ခင္ ၾကိဳျပီးသတ္မွတ္ေပးသင္႔ပါတယ္
တစ္ကယ္လို႔  string တစ္ခုမွာ new နဲ႔ သံုးထားတဲ႔ class ရဲ႕ name ပါ၀င္ခ႔ဲရင္ new instance က အဲဒီ class ကိုဖန္တီးပါတယ္
class က namespace တစ္ခုထဲမွာဆိုရင္ေတာ႔  qualified name ကိုအသံုးျပဳရမွာပါ

instance တစ္ခုဖန္တီးျခင္း
<?php
$instance=new SimpleClass();

//This can also be done with a variable:
$className='Foo';
$instance = new $className(); // Foo()
?>
class ထဲမွာ new self and new parent နဲ႔ object အသစ္တစ္ခုဖန္တီးဖို႔ျဖစ္နိုင္ပါတယ္
new variable တစ္ခုထဲကို  class တစ္ခုရဲ႕ instance လုပ္ထားတဲ႔ ဟာကို assign လုပ္တဲ႔အခါမွာ
new variable က တူညီတဲ႔  assing လုပ္တာကို object အျဖစ္နဲ႔ လက္ခံပါတယ္ ဒီလိုလုပ္တာဟာ
function တစ္ခုကို instance လုပ္တာနဲ႔ တူူပါတယ္
အသင္႔ဖန္တီးထားတဲ႔ object တစ္ခုကို cloning လုပ္ျပီး copy လုပ္နိုင္ပါတယ္
Object Assignment
<?php
$instance = new SimpleClass();
$assigned = $instance;
$reference =& $instance;
$instance->var = '$assigned will have this value';
$instance = null // $instance and $reference  become null
var_dump($instance);
var_dump($reference);
var_dump($assigned);
?>
The Output is
NULL
NULL
object(SimpleClass)#1 (1) {
   ["var"]=>
     string(30) "$assigned will have this value"
}

new Object ဖန္တီးျခင္း
<?php
class Test
{
    static public function getNew()
    {
        return new static;
    }
}
class Child extends  Test
{
}
$obj1 = new Test();
$obj2 = new $obj1;
var_dump( $obj1 !== $obj2 );

$obj3 = Test::getNew();
var_dump($obj3 instanceof Test);

$obj4 = Child::getNew();
var_dump($obj4 instanceof Child);
?>
The Output is
bool(true)
bool(true)
bool(true)

extends
class တစ္ခုဟာ class declaration ထဲမွာ extends keyword ကိုအသံုးျပဳျပီး အျခား class ရဲ႕ properties နဲ႔ method
ကို  inherit လုပ္နိုင္ပါတယ္ muliple classes ကို ရည္ညြန္းဖို႔အတြက္ေတာ႔မရပါဘူး သူရဲ႕ base class ကေနသာ
class ကို inherit လုပ္နုိင္ပါတယ္ inherite လုပ္ခဲ႔တဲ႔  method and property ဟာ parent class ထဲမွာ
တူညီတဲ႔ name နဲ႔႔ပဲ႔ redeclaring လုပ္ျပီး overridden လုပ္နိုင္ပါတယ္ သို႔ေသာ္လည္း
parent class က final method နဲ႔ define လုပ္ခဲ႔မယ္ဆိုရင္ ဒီ method ကို overriden လုပ္လို႔မရပါဘူး
overriden method ကို access လုပ္ဖို႔ parent:: ကိုသံုးရင္ေတာ႔ျဖစ္နိုင္ပါတယ္

Simple Class Inheritance
<?php
    class ExtendClass extends SimpleClass
    {
        function displayVar()
        {
            echo "Extending class \n";
            parent::displayVar();
        }
    }
    $extended = new ExtendClass();
    $extended->displayVar();
?>
The Output is
Extending class
a default value

written by cyberoot.blogspot.com

No comments:

Post a Comment

Thanks for your comments
Welcome from cyberoot