study note of "Zend PHP 5 Certification Study "--OOP

study note of "Zend PHP 5 Certification Study "--OOP
author: jeff_zeng
date:2009.12.15
Email:zengjiansheng1@126.com

QQ:190678908
MSN:zengjiansheng1@hotmail.com
blog:http://blog.youkuaiyun.com/newjueqi

一. OOP

note:  I just write down what is differeance between java and php in OOP.

1. Public variable can with key word "public" or "var",but in order to Compatible php6,please use "public" commonly.
 
2. The two styles of construct methods in php:
a,function _construct().
b,just like C++ and java ,use class name as construct function's name.

   Note that , if the _construct method is not founded, php will find the php4 old_style construct(the class name's method)  
and call it instead.


3.php's decontruct function  like

function _decontruct(){
.....
}

  The decontruct function is called right before the object is destoryed,and is useful for performing cleanup procedures--
such as deleteing the temp files,or disconnect from the remote resource.


4. A member function with the "final" keyword,can not be overloaded in the extend class(include have different parameters).

for example:
class myTestClass{
        
        final public function showMessage(){
            echo "1<br>";
        }
        
    }
    
    class myTestClass1 extends myTestClass {
        
        public function showMessage($var){
            echo "2<br>";
        }
    }
    
    $my=new myTestClass1();
    $my->showMessage('ds'); //wrong,output the message "Cannot override final method "


5.self keyword

   Allow you to access the static member  in the class, example in point 7.

6.static keyword

  The static function just accesses the static member varialbe or other static function ,because the static function set before
the class instance,so if the static function can access other non static function or variable, there may be a question: the
non static function or variable is not set in the memory, and the static member should be assignment when declare .

for example:
    class testSelfWord{
    
        public static $name="jack";

        function _construct($name){
            $this->name=$name; //invalid
            echo $this->name; //invalid , cannot output anything,because $name is the static variable
        }
        
        function showMessage(){
            echo self::$name;
        //    echo $this->name; //invalid , cannot display anything
        }
        
    }
    
    $my =new testSelfWord("tom");
    $my->showMessage();    //output "tom"

7. const

   Define the const variable in the class,note that the const variable's name do not include the keyword "$".
   It can use the const by "classname::constname".

for example :
        class constClass{
        
        const PI="3.14"; ////note,do not include the

keyword "$"
        
        public function show(){
            echo PI;
        }
        
    }
    
    $my=new constClass();
    $my->show(); //output "PI", not "3.14"
    echo constClass::PI; //output "3.14"


    At the same time , we compare with another define const variable way which be used out of the class,let me kown the
difference of them.

for example:

//note,do not include the keyword "$"
    define("myvar","tom");
    echo myvar;

8.Interface

   Interface is used specify an API that a class must implement.This allow you to create a common contract that your class
must be implemented in order to satisfy special centain logical requrment-like  you can use interface to abstract the concept of the
database privider to a common that could then be implement by series classes that inferface to differet DBMS.
  Note that the interface's methods don't contain body.

9.abstract class

  The abstract class with a keyword "abstract" , an abstract class must contain an abstract method(a method don't contain body and
with the keyword "abstract").
  Abstract classes cannot be use directly, but they must be extends so the extend class privides a full complement of methods.

10.instanceof

   The instanceof keyword judge a instance is the instant of a class .

for example:

class constClass{
        
        const PI="3.14"; //note:do not include the keyword "$"    
        public function show(){
            echo PI;
        }        
    }
    
    $my=new constClass();
    echo ($my instanceof constClass); //output 1, show that variable "PI" is the instant of constclass
    
11._tostring()

  When you echo the class info, you can override the _tostring() method which could set the output message  .

for example:

    class toStringClass{
        
        /*
         * note that the _tostring() must return avalue
         */
        function __tostring(){
            return "dsdfsdf";
        }
        
    }
    
    $my=new toStringClass();
    echo $my;
    
12.__call($funname,$arr_value)

  The function will be called when you call the member's function which is not exist .You can override the __call() to
deal with this question.
  Note that parameter $arr_value is a array, beacuse the number of  parmeters  in the function may be many.

for example:
    
    class testCallClass
    {
        function __call($funName,$funPar){
              echo "wrong function name:".$funName.'<br>';
            echo print_r($funPar);
          }
                   
    }
    
    $my=new testCallClass();
    $my->showHello("11","aa");
    //output:wrong function name:showHello
       //Array ( [0] => 11 [1] => aa ) 1


13.__clone()  

   in php , _clone() is used to a object.

for example:

    class testCloneClass{
        
        public $var;
        
        function __construct($var)
        {
            $this->var=$var;
        }
        
        function  showMessage(){
            echo $this->var.'<br>';
        }
        
         function __clone(){
            echo "call when the object clone<br>";

    
         }
        
    }
    
    $a=new testCloneClass("tom");
    $a->showMessage();//display "tom"
    $b=clone $a; //display "call when the object clone"
    $b->showMessage(); //display "tom"
    
    //The following program show that $a and $b is two different instance,so it is a clone.
    //$a and $b not point to the same instance  
    $b->var="jack";
    $a->showMessage();//display "tom"
    $b->showMessage(); //display "jack"

14.__get()and __set()

  When you get or set the class member variable is not exist or the variable is private, PHP will call the the __get() or __set() function.

for example:
    class testSetGetClass{
        
        private $name;
        
        public function _construct($name){
            
            $this->name=$name;
        }
            
        function __get($varName){
            echo "call the get var:$varname <br>";
                        
            return $this->$varName;
        }
        
        function __set( $varName, $varValue){
            echo "call the set varname:$varname,varValue:$varValue <br>";
            $this->$varName=$varValue;
        }
        
    }
    
    $my=new testSetGetClass("Tom");
    
    //set the private variable, PHP would call __set() function
    //output "call the set varname:,varValue:jack
    $my->name="jack";

    //access the private variable , php will call __get() function
    //output "call the get var: "
    //output "jack"
    echo $my->name."<br>";
    
    //set a variable which didn't exist ,PHP will call __set() function
    //output "call the set varname:,varValue:23 "
    $my->age=23;
    
    //get a variable which didn't exist, PHP will call __get() function
    //output "call the get var: "
    echo $my->id;

From this example we can know that :

1. When  call the __get function, the variable's name is not
passing into the parameter.
2. When  call the __set function, the variable's name is passing
into the parameter, but the variable's value is not .

15._autoload

   In a large Project, there may be many class files,we must include the class file before we want to instance a class, if
has many class files, it may be write "include *.php" many times, so PHP privode a function "_autoload" to solve this
question.
   Note that this _autoload function is write outside of the class.

for example:

The ClassA file "ClassA.php"
class ClassA
{
    function showMessage(){
        echo "this is class a";
    }
    
}

  Then we instance the class ClassA in another php file and override the __autoload function:

   function __autoload($class_n){

  include($class_n.".php");
 
   }
    
   $a=new ClassA();//if you do not override __autoload,it will output a error message "Class 'ClassA' not found".
   
   $a->showMessage();

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

newjueqi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值