Underscore method prefix

后端 未结 7 2047
挽巷
挽巷 2021-01-06 00:12

I\'ve been examining the code of CodeIgniter and CakePHP and I noticed that some of the methods in their classes are prefixed with an underscore _ or a double u

7条回答
  •  臣服心动
    2021-01-06 00:46

    In Codeigniter, methods within controllers can normally be called as part of the url so you might call the method "index" in controller "main" as follows:

    mysite.com/main/index.

    You might also have a method within your controller that you don't want someone to be able to call as a segment in the url, so you would prefix it with a "_" (single underscore) .. that is different than making it private. Private methods are only callable within the class where it is defined. So a controller method could be prefixed with an underscore which would make it uncallable as a url segment and it could also be declared private which would make it uncallable from other classes.

提交回复
热议问题