codeigniter-4

understanding of controller function syntax in ci4


I've a simple question about the syntax of the functions inside the controller.

For example:

public function index(): string

I figure out that "public" means that I can call and access this function by user/browser, "function index()" is clear define as as function with name "index".

But what is ": string" - for that is my question, what is this and how it works?

Looking at the ci4 docu brings only the understandings like I explained.


Solution

  • Codeigniter is written in php. In php, this value after the colon is the return type. Your example function will return a String. See the example in the php manual.

    Like parameter types, return types are optional, but allow the compiler to check that caller and called function agree on what is passed and returned to catch errors early.