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

php学习笔记(一)————php类的概念

时间:2017-06-15 17:19:21      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:eth   else   效果   get   image   分享   使用   属性   基本   

<?php
//类的概念
/*
 * 一个类包含自己的属性和函数
 * 
 * 属性:属于类自己的常量和变量
 * 
 * 方法:就是函数
 * 
 * 类是一类事物的抽象
 */
//例子:
//车就是一种抽象
class Car{
    //车的基本属性:有轮子,有颜色,可以拉货(人)    
    public $color = ‘red‘;//默认红色
    public $wheel_size = 4;//默认4个
    
    //方法:拉货默认
    public function pull_some_thing($something = ‘货‘){
        echo "我是一辆 $this->color 色的,有 $this->wheel_size 个轮子的车,我正在拉 $something ";
    }
}

$qiche  = new Car();
$qiche->color = ‘黑‘;
$qiche->wheel_size = 12;
$qiche->pull_some_thing(‘人‘);

echo ‘<hr/>‘;


//例子2:
class A
{
    function foo()
    {
        if (isset($this)) {
            echo ‘$this is defined (‘;
            echo get_class($this);//获得这个$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();//在b类下调用A类的foo方法,获得的$this 是B类
    }
}

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

@A::foo();//直接在类外调用A类的foo方法不能获取到A类(就是说不能在类外使用$this指代目标类)
//$this->foo();//会报错

$b = new B();
@$b->bar();

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

echo ‘<br/>‘;

执行效果 

技术分享

php学习笔记(一)————php类的概念

标签:eth   else   效果   get   image   分享   使用   属性   基本   

原文地址:http://www.cnblogs.com/wobeinianqing/p/7018634.html

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