Home > Article > Backend Development > Summary of naming conventions in PHP programming
This article introduces the naming conventions in PHP programming, including basic concepts, camel-style nomenclature, camel case method, and Pascal method. Friends in need may wish to refer to it.
Camel case naming (also known as camel case naming), as its name CamelCase indicates, refers to the use of a mixture of uppercase and lowercase letters to form the names of variables and functions. In order to make it easier for programmers to communicate their code among peers, they often adopt a unified naming method that is more readable. For example: some programmers like to use all lowercase letters, and some programmers like to use underscores, so if they want to write a variable of my name, their common writing methods will be myname, my_name, MyName or myName. Such naming rules are not suitable for all programmers to read, but using camel case naming can increase the readability of the program. For example, here is the same function named using camel notation and underscore notation: printEmployeePaychecks(); print_employee_paychecks();The first function name uses camel nomenclature - each logical breakpoint in the function name is marked with an uppercase letter; The second function name uses the underscore method - each logical breakpoint in the function name is marked with an underscore. The underscore method became popular after the emergence of c. It is very commonly used in many old programs and environments such as UNIX. Camel-Case is a set of naming rules (conventions) when writing computer programs. Camel-style nomenclature is when a variable name or function name is a unique identifier composed of one or more single words connected together. The first word starts with a lowercase letter; the first letter of the second word is capitalized or each letter is capitalized. The first letters of a word are all capital letters, for example: myFirstName, myLastName. Such variable names look like camel humps rising one after another, hence the name. The term Camel-Case comes from the mixed-case format commonly used in the Perl language, and the cover picture of the best-selling book "Programming Perl" (published by O'Reilly) written by Larry Wall et al. A camel. The naming rules of camel nomenclature can be regarded as a convention, not absolute or mandatory, in order to increase recognition and readability. Hump method (small hump method) Variables are generally identified using camel case. CamelCase means capitalizing the first letter of every word except the first word. For example: int myStudentCount;The first word of the variable myStudentCount is all lowercase, and the first letter of the following words is capitalized. Pascal method (big camel method) Compared with the small camel case method, the large camel case method also capitalizes the first letter of the first word. Commonly used in class names, function names, attributes, and namespaces. For example: publicclass DataBaseUser;Attached, php naming rules Proper naming is at the heart of program planning. The name is a long-term and far-reaching result of something in the ecological environment in which it is located. In general, only programmers who understand the system can come up with the most appropriate name for the system. If all names are naturally appropriate, the relationship is clear, the meaning can be deduced, and ordinary people's inferences can be expected. If you find that only a few of your names match their corresponding objects, it’s best to take a good look at your design again. Class naming Before naming a class, you must first know what it is. If you still can't remember what this class is based on the clues provided by the class name, then your design is not good enough. Mixed names consisting of more than three words can easily cause confusion between various entities in the system. Take a look at your design and try to use (CRC Se-ssion card) to see if the entity corresponding to the name has so many functions. When naming a derived class, you should avoid the temptation to use the name of its parent class. The name of a class is only related to itself and has nothing to do with the name of its parent class. Sometimes suffixes are useful. For example: if your system uses an agent, then name a component "DownloadAgent" to actually transmit information. Method and function naming Usually each method and function performs an action, so their naming should clearly indicate what they do: use CheckForErrors() instead of ErrorCheck(), and use DumpDataToFile() instead of DataFile(). Doing so also makes functions and data more distinguishable objects. Sometimes suffixes are useful: Max - means the maximum value that can be assigned to an entity. Cnt - The current value of a running count variable. Key - key value. For example: RetryMax represents the maximum number of retries, and RetryCnt represents the current number of retries. Sometimes prefixes are useful: Is - means to ask a question about something. Whenever people see Is they know it's a problem. Get - means to get a value. Set - means to set a value For example: IsHitRetryLimit. Do not use all capital letters for abbreviations In any case, when you encounter the following situations, you can use the first letter in uppercase and the remaining letters in lowercase instead of using all uppercase letters to express: abbreviation. Use: GetHtmlStatistic. Not used: GetHTMLStatistic. Reason People seem to have very different intuitions when names contain abbreviations. It is best to have unified regulations, so that the meaning of the naming is completely predictable. Take the example of NetworkABCKey. Note whether C should be the C in ABC or the C in key. This is very confusing. Some people don't care, others hate it. So you will see different rules in different codes, so you don't know what to call it. For example class FluidOz // Do not write FluidOZ class GetHtmlStatistic // Do not write GetHTMLStatisticClass naming Use uppercase letters as word separators and all other letters in lowercase Capitalize first letter of name Do not use underscore ('_') reason According to many naming methods, most people think this is the best way. For example class NameOneTwoclass name Class library naming Namespaces are currently being used more and more widely to avoid class name conflicts between class libraries from different vendors and groups. When namespaces have not yet been adopted, in order to avoid class name conflicts, the general approach is to add a unique prefix before the class name. Two characters is enough. Of course, it would be better to use more. For example John Johnson's data structure class library can be prefixed with Jj, as follows: classJjLinkList { }Method naming Adopt consistent rules for class naming reason Most people using all the different rules find this to be the best compromise. For example class NameOneTwo { function DoIt() {}; function HandleError() {}; }Class attribute naming Property names should be prefixed with the character 'm'. The prefix 'm' follows a consistent rule for class naming. 'm' always modifies the beginning of a name, just as 'r' represents a reference. reason Prefix 'm' prevents any collision of class attribute and method names. Your method names and property names will often be very similar, especially when accessing elements. For example class NameOneTwo { function VarAbc() {}; function ErrorNumber() {}; var mVarAbc; var mErrorNumber; var mrName; }Parameter naming in methods Use lowercase letters for the first character. All words after the first character are capitalized according to class naming rules. reason You can always know which variable corresponds to which variable. You can use a name similar to the class name without causing a name conflict. For example class NameOneTwo { function StartYourEngines( &$rSomeEngine, &$rAnotherEngine); }Variable naming Use lowercase for all letters Use '_' as a delimiter for each word. reason In this way, the scope of variables in the code is clear. All variables look different in the code and are easily identifiable. For example function HandleError($errorNumber) { $error = OsErr(); $time_of_error = OsErr->getTimeOfError; $error_processor = OsErr->getErrorProcessor; }Reference variables and functions returning references References must be prefixed with 'r' reason Make variables of different types easier to identify It determines which method returns a mutable object and which method returns an immutable object. For example class Test { var mrStatus; function DoSomething(&$rStatus) {}; function &rStatus() {}; }Global variables Global variables should be prefixed with 'g'. reason It is very important to know the scope of a variable. For example global $gLog; global &$grLog;Define named/global constants Global constants separate each word with '_'. reason This is the tradition of naming global constants. You should be careful not to conflict with other definitions. For example define("A_GLOBAL_CONSTANT", "Hello world!";Static variables Static variables should be prefixed with 's'. reason It is very important to know the scope of a variable. For example function test(){ static $msStatus = 0; }Function naming Function names follow the C GNU convention, using all lowercase letters and using '_' to separate words. reason This makes it easier to distinguish related class names. For example function some_bloody_function() { }Error return detection rules Check all system calls for error messages, unless you want to ignore errors. Define the system error text for each system error message for include. Braces {} rules Of the three main brace placement rules, two are acceptable, the first of which is the best: Place the braces in the same column below the keyword: if ($condition) while ($condition) { { ... ... } }The traditional UNIX bracket rules are that the first bracket is in the same column as the keyword, and the last bracket is in the same column as the keyword: if ($condition) { while ($condition) { ... ... } }Reason Non-principled issues that cause fierce debate can be solved through compromise. Either method is acceptable. However, for most cases, Most people prefer the first one. The reason is something in the field of psychological research and study. There are more reasons to prefer the first one. If the character editor you are using supports the bracket matching function (such as vi), it is best to The important thing is to have a good style. Why? We say that when you have a large program and want to know where this large program is ended words. You first move to the opening bracket, and after pressing the button the editor will find the corresponding closing bracket, for example: if ($very_long_condition && $second_very_long_condition) { ... } else if (...) { ... }To move from one program block to another, just use the cursor and your bracket matching key. There is no need to move back and forth to the end of the line to find the matching bracket. |