类的定义和使用

类的定义

class myClass{
}

 

array(
    'type'=>'class',
    'name'=>'myClass',
    'child'=>array(
    )
)

所有的属性和方法,都放在child里面

添加属性,比如我们给myClass类添加一个$name的属性

class myClass{
        public $name="Hello";
}

 

array(
    'type'=>'class',
    'name'=>'myClass',
    'child'=>array(
        array(
            'type'=>'property',
            'name'=>'$name',
            'public'=>true,
            'value'=>array(
                'type'=>'string',
                'data'=>'Hello'
            )
        )
    )
)

属性的type=property,name保存属性名称,value是属性的默认值

public=true是说明属性是public类型的,如果属性是protected或者private类型的,则会有protecetd=>true和private=>属性

这一类描述属性和方法的属性,总共有:private、protected、public、static、final

添加方法,添加方法和普通的函数添加语法是一样的。

class myClass{
        private $name="Hello";
        public function show(){
        }
}

 

对应metaPHP代码

array(
    'type'=>'class',
    'name'=>'myClass',
    'child'=>array(
        array(
            'type'=>'property',
            'name'=>'$name',
            'public'=>true,
            'value'=>array('type'=>'string','data'=>'Hello')
        ),
        array(
            'type'=>'function',
            'public'=>true,
            'name'=>'show',
            'property'=>array(),
            'propertyType'=>array(),
            'child'=>array(),
        ),
    )
)

 

和普通的function定义方式是一样的,拥有name、property、propertyType、child属性。唯一的不同是他拥有了public=>true描述,是根据函数类型定义对应的public、protecetd、private


 

类的继承 和类的接口实现

比如下面这个类,定义了继承parentClass类

class myClass extends parentClass{
        public function show(){
        }
}

 

 则需要给结构增加属性extends属性

array(
    'type'=>'class',
    'name'=>'myClass',
    'extends'=>'parentClass',
    'child'=>array(
        array(
            'type'=>'function',
            'public'=>true,
            'name'=>'show',
            'property'=>array(),
            'propertyType'=>array(),
            'child'=>array(),
        ),
    )
)

 

实现接口,如下面这个例子,需要实现解耦interface1

class myClass implements interface1{
        public function show(){
        }
}

 

那么与继承类似,需要给class结构增加implements属性

array(
    'type'=>'class',
    'name'=>'myClass',
    'implements'=>'interface1',
    'child'=>array(
        array(
            'type'=>'function',
            'public'=>true,
            'name'=>'show',
            'property'=>array(),
            'propertyType'=>array(),
            'child'=>array(),
        ),
    )
)

 

类的使用

生成实例

$handle = new myClass("Hello");

 

 

则对应metaPHP代码是

//$handle = new myClass("Hello");
array(
    'type'=>'=',
    'object1'=>array(
        'type'=>'variable',
        'name'=>'$handle'
    ),
    'object2'=>array(
        'type'=>'new',
        'name'=>'myClass',
        'property'=>array(
            array('type'=>'string' , 'data'=>'Hello'),
        )
    )
)

 

type=new代表的是new运算,name表示创建的类名,property表示初始化时的参数

调用静态方法staticFunction

oneClass::init("Hello");

 

对应为

array(
    'type'=>'staticFunction',
    'object'=>'oneClass',//类名
    'name'=>'init',//函数名
    'property'=>array(
        array('type'=>'string' , 'data'=>'Hello'),
    )
)

 

调用实例方法

$obj = new myClass();
$obj->func1('Hello');

$obj->func1这一句转换为

array(
    'type'=>'objectFunction',
    'object'=>array( 'type'=>'variable' , 'name'=>'$obj' ),
    'name'=>'func1',
    'property'=>array(
        array( 'type'=>'string' , 'data'=>'Hello' )
    )
)

 

 


 

 

parent、this和self引用

可以直接用type=>this、type=>parent和type=>self的对象来表示

class myClass{
        public function show(){
                parent::init();
        }
}

 

 

array(
    'type'=>'class',
    'name'=>'myClass',
    'child'=>array(
        array(
            'type'=>'function',
            'public'=>true,
            'name'=>'show',
            'property'=>array(),
            'child'=>array(
                array(
                    'type'=>'staticFunction',
                    'object'=>array(
                        'type'=>'parent',
                    ),
                    'name'=>'init'
                )
            ),
        ),
    )
)

 

目录

引擎介绍

元代码结构

metaPHP数组的使用

函数的使用

类的定义和使用

杂七杂八的

修改已有代码

也欢迎进行QQ联系本人 ,交流我在开发metaPHP过程中的经验

QQ:309568486

posted @ 2017-09-22 14:39  浩然哥哥v5  阅读(306)  评论(0编辑  收藏  举报