0

I saw many tutorials underscores (_) are used in function.

But I don't know why it is used and how can I use ?

Like this :

function _save_cart()
{

}
Manish Tiwari
  • 1,806
  • 10
  • 42
  • 65
  • Question has been answered here. http://stackoverflow.com/questions/663350/whats-the-deal-with-a-leading-underscore-in-php-class-methods – dbushy727 Feb 09 '16 at 03:37

4 Answers4

1

According to Codeigniter documentation,

Prefixing method names with an underscore will also prevent them from being called. This is a legacy feature that is left for backwards-compatibility.

I have used it in cases when I need a helper-type function to be publicly accessible within the code, such as for validation, but not accessible from a browser.

Enoch
  • 202
  • 1
  • 10
0

Underscores are a perfectly valid character in PHP function names and you can use them like any other.

Generally, a function that starts with an underscore means that the function is meant to have a hidden visibility. Here is an example with the WordPress source code.

At the same time, an underscore might just be used to reduce the chance of a naming conflict or just because the author thought it would be more aesthetically pleasing. Here is another example with WordPress (a function that is meant to be used publicly):

As mentioned in the comments there is another answer regarding methods. Underscores in front of functions and methods are considered bad practice. For more information you can refer to the PSR-2 Coding Style Guide.

iridian
  • 669
  • 5
  • 13
0

In objective languages functions and variables names seperate with capitalize char, forexample: saveCart(), getCart(), ...

But in functional languages people use underscore and i think it comes from C posix standarts/notations.

C language doesn't support private and protected keywords so people generally use one or two underscore as pre extension for make the function private or protected.

Php firstly created as a functional language after a long time ZEND added object oriented features. So many people used to write it like a functional language. And so if you see a function like _save_cart you can understand that: an old school php coder make this function protected or private with posix way.

WinterCoder
  • 106
  • 1
  • 4
0

There are 4 naming conventions:

  1. with_underscores
  2. PascalCased ( first letter is caps and second word is caps / Pascal+Cased / ContactUs)
  3. camelCased ( first letter is small caps and second is caps / cammel+Casesd / contactUs)
  4. lower_case ( all the letters are small caps / contact_us)

But in CI we use PascalCased and lower_case


In Codeigniter

---------------------------------
| Classes       |   PascalCased |
| Methods       |   lower_case  |
| Properties    |   lower_case  |
| Functions     |   lower_case  |
| Variables     |   lower_case  | 
---------------------------------

Read this

  1. Codeigniter Variable Names
  2. PSR-2: Coding Style Guide
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85