为什么我需要在Controller中使用Init功能?

I am new to PHP and I have a few questions that follows:

Do I need the init function or can I do the job (whatever I need to do in my code) without the init function?

I am saying this because the NetBeans "kinda" created/added automatically the init() function in my project.

In my code I am suppose to create the CRUD functionality in it.

If I don't use it what's the problems I might have and the downsides?

If you are new to PHP, do not start by using a framework. Instead you should learn the language itself.

There is nothing significant about init() function. It is not a requirement for classes in PHP. Hell .. even __construct() is not mandatory in PHP.

That said, Zend Framework executes it right after the controller is created. It is required if you are using ZF. You can read more about it here.

As the official docs would say:

The init() method is primarily intended for extending the constructor. Typically, your constructor should simply set object state, and not perform much logic. This might include initializing resources used in the controller (such as models, configuration objects, etc.), or assigning values retrieved from the front controller, bootstrap, or a registry.

You can have controllers that don't override the init() method, but it will be called under the sheets anyways.

Simply its a constructor for that class(controller)...

init(){
$this->a = 1; //If we set something like this in the init
}

public function fooAction(){
echo $this->a; //1
}
public function barAction(){
echo $this->a; //1
}

ie the variables,objects..that is initialised in init will be available to all the actions in that controller

init() in Zend_Framework for most practical purposes is where you would put code that you need to affect all of the actions in that controller.(at least to test against all of the actions).

For example I often use the init() method to set up the the flashmessenger helper and to set the session namespace I want to be used.:

public function init() {

        if ($this->_helper->FlashMessenger->hasMessages()) {
            $this->view->messages = $this->_helper->FlashMessenger->getMessages();
        }
        //set the session namespace to property for easier access
        $this->_session  = new Zend_Session_Namespace('location');

    }

Also Netbeans did not make this method or the controller, Zend_Tool made the controller and the methods utilizing the interface that Netbeans provided. That's why in your PHP settings for Netbeans you have to provide the path to the ZF.bat file and click the register provider button if you change your ZF install.

One more thing...Be aware that there more methods available to the controller that provide hooks into different parts of the dispatch cycle. You may not need them very often but you need to know they are there.