开发者

Can php have an interface inside a class?

i would like to know if it is possible to have a function in PHP which returns an interface or a class which contains an interface?

i tried something like this, but it fails

<?php
//class for list of controllers for ACL
class Gestionale_Action_Helper_Crud extends Zend_Controller_Action_Helper_Abstract {

     interface crud_controller
    {
        public function indexAction();
        public function modificaAction();
        public function cancellaAct开发者_JAVA技巧ion();
        public function creaAction();
    }


    public function getCrudInterface(){
        return $this->crud_controller;
    }
}

what i wanted to do, in zend framework, create an interface that crud controllers must implement, or even better if i could create an abstract controller and have them implement that

thank you


I'd suggest that you use Zend_Rest_Controller instead of creating your own interface.

Zend_Rest_Controller is an abstract class that defines five basic methods you need in a CRUD-controller: index, get, post, put, and delete.

Combined with Zend_Rest_Route it lets you create nice and clean RESTful application.

You can get more reading on Zend_Rest_Controller at http://weierophinney.net/matthew/archives/228-Building-RESTful-Services-with-Zend-Framework.html and http://techchorus.net/create-restful-applications-using-zend-framework


Just place the interface outside of any class (preferably in a different file) and let it be implemented by all your crud-controllers.

<?php
class GrudController implements CrudInterface
{
    // ...
}


i'm not sure i get what it is you want to do, but i'm fairly certain you're asking the wrong question. if you simply want to make sure an object implements a certain interface, this is quite easy to do. lets say for example you have some helper method in a class which deals with a crud controller, you just specify the type in the argument list:

class crud_helper {
    public function help(crud_controller $cc) {
        $cc->indexAction();
    }
}

now you can pass any object that is an instance of a class that implements crud_controller to the method help. but no other object.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜