码迷,mamicode.com
首页 > Web开发 > 详细

php中的$this用法

时间:2015-05-27 15:23:22      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:

在实例化中:
普通方法内通过 $this->functionName();可以调用三种模式的静态方法和三种模式的普通方法

<?php
/**
 * 
 * @authors HG (hg0728@qq.com)
 * @date    2015-05-26 17:12:02
 * @version 1.0
 */
header("Content-type:text/html;charset=utf-8");
class A {

    function f(){
        //echo ‘这是类A的普通方法func()<br />‘;
        $this->stc();
        $this->stc_pe();
        $this->stc_pd();
        $this->func();
        $this->pe_func();
        $this->pc_func();
    }

    
    static function stc(){
        echo ‘这是类A的静态普通方法<br />‘;
        
    }
    static private function stc_pe(){
        echo ‘这是类A的静态私有方法<br />‘;
        
    }
    static protected function stc_pd(){
        echo ‘这是类A的静态受保护方法<br />‘;
        
    }
    function func() {
        echo ‘这是类A的普通方法<br />‘;
        
    }
    private function pe_func(){
        echo ‘这是类A的私有方法<br />‘;
        
    }
    protected function pd_func(){
        echo ‘这是类A的受保护方法<br />‘;
        
    }
    public function pc_func(){
        echo ‘这是类A的公共方法<br />‘;
        
    }
}

$a = new A();

$a->f();

/*
在实例化中
普通方法内通过 $this->functionName();可以调用三种模式的静态方法和三种模式的普通方法

*/

在实例化:

静态方法中通过

self::functionName();可以调用三种模式的静态方法
不能使用$this->functionName();调用任何方法

技术分享
<?php
/**
 * 
 * @authors HG (hg0728@qq.com)
 * @date    2015-05-26 17:12:02
 * @version 1.0
 */
header("Content-type:text/html;charset=utf-8");
class A {

    static function f(){
        echo ‘这是类A<br>‘;
        self::stc();
        self::func();//报错
        // $this->stc_pe();
        // $this->stc_pd();
        // $this->func();
        // $this->pe_func();
        // $this->pc_func();
    }

    
    static function stc(){
        echo ‘这是类A的静态普通方法<br />‘;
        
    }
    static private function stc_pe(){
        echo ‘这是类A的静态私有方法<br />‘;
        
    }
    static protected function stc_pd(){
        echo ‘这是类A的静态受保护方法<br />‘;
        
    }
    function func() {
        echo ‘这是类A的普通方法<br />‘;
        
    }
    private function pe_func(){
        echo ‘这是类A的私有方法<br />‘;
        
    }
    protected function pd_func(){
        echo ‘这是类A的受保护方法<br />‘;
        
    }
    public function pc_func(){
        echo ‘这是类A的公共方法<br />‘;
        
    }
}

$a = new A();

$a->f();

/*
在实例化中
静态方法内通过 
self::functionName();可以调用三种模式的静态方法
不能使用$this->functionName();调用任何方法

*/
View Code

 

php中的$this用法

标签:

原文地址:http://www.cnblogs.com/ahhg/p/4533333.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!