用户
 找回密码
 入住 CI 中国社区
搜索
查看: 3401|回复: 3
收起左侧

[讨论/交流] Ci2.0新特性Driver类

[复制链接]
发表于 2010-5-7 08:15:45 | 显示全部楼层 |阅读模式
翻译一下
Drivers

新的ci中有一种新的类叫做Driver.Ci通过这个类可以使一个父类带有多个子类。子类可以访问父类,而子类之间不能互相访问。
There’s a new type of library known as a driver. Essentially, it’s a way of having a parent class with many child classes. Child classes are able to access the parent class, but not its sibilings. For example, here’s how you might use one:

这里some_parent是你想调用的父类
// Where class name is the name of the driver class you want to invoke. For example, to load a driver named "Some Parent" you would do this:
$this->load->driver('some_parent');
父类的方法可以这样被调用:
// Methods of that class can then be invoked with:
$this->some_parent->some_method();

子类可以直接通过父类调用,不需要实例化。(子类的实例成为了父类的属性)

// The child classes, the drivers themselves, can then be called directly through the parent class, without initializing them:
$this->some_parent->child_one->some_method();
$this->some_parent->child_two->another_method();


I’m quite excited about this, it means that grouping code with similar functionality would be easier, for example:

$this->load->driver('api');

$this->api->twitter->call('statuses/update', array('update' => 'Nice status!'));
$this->api->facebook->get_friends();
$this->api->vimeo->get_videos($user);//更好的组织代码

I think drivers will be a great new way for developers, such as myself, to release a ‘package’ of libraries that all do similar things (for example some of my API libraries). Drivers will also help us keep code DRY (Don’t repeat yourself). Many of the libraries we write share common code among them, and could be grouped into a bundle of libraries with similar code, functionality, and usage. These would be perfect to use drivers for.
//我想这使得Ci2.0更加面向对象了,从而使得代码更加整洁,DRY!
发表于 2010-5-7 09:15:58 | 显示全部楼层
你这是在哪看到的新闻,我找了半天也没找到
 楼主| 发表于 2010-5-7 12:46:24 | 显示全部楼层
回复 2# suxiaolu
http://www.haughin.com/2010/03/11/codeigniter-2-critical-changes-implications/
发表于 2010-5-7 13:29:33 | 显示全部楼层
充分利用php5的特性啊,赞一个

本版积分规则