PHP class declaration method and code layout

Source: Internet
Author: User

Declaration of a class

When writing a class, you should adhere to the following formatting rules:

-Do not have spaces between the opening parenthesis "(") before the method name and its argument list
-The opening brace "{" is at the end of the statement peer
-The right Brace "}" is on a separate line, aligned with the corresponding declaration statement, unless it is an empty statement, "}" should be immediately after "{"

Class Sample extends Object {
int $ivar 1;
int $ivar 2;

function Sample (int $i, int $j) {
ivar$1 = $i;
ivar$2 = $j;
}

function Emptymethod () {}

...
}

-Null line separation between method and method

Layout

Declare a variable only at the beginning of the code block. (a block refers to any code that is enclosed in the middle of the braces "{" and "}".) Do not declare the variable when it is first used. This can confuse programmers who are distracted, while interfering with the portability of your code within that scope.


function MyMethod () {
int $int 1 = 0; Start of Method block

if ($condition) {
int $int 2 = 0; The start of the "if" block
...
}
}


An exception to this rule is the index variable for the FOR loop


for (int $i = 0; i < $maxLoops; $i + +) {...}


Avoid declaring a local variable that overrides a variable declared at the previous level. For example, do not declare the same variable name in an internal code block:


int $count;
...
function MyMethod () {
if ($condition) {
int $count = 0; Avoid such statements
...
}
...
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.