-
Notifications
You must be signed in to change notification settings - Fork 0
Getting started: Coding standards
The following coding standards have been used when developing Mako. We suggest you follow the same standards when developing applications using Mako.
Always use long open tags. Never use short tags or ASP style tags. Class files should never include a closing tag.
// Correct
<?php
// Incorrect
<?
// Incorect
<%
Files should always have the same name as the class they contain. A file should never contain more than one class. The file encoding should always be UTF-8.
File names must match the case of the class. The only exception being controllers and tasks, which should always have lower case file names.
// File: libraries/mako/DateTime.php
<?php
namespace mako
{
class DateTime
{
}
}
// File: app/controllers/index.php
<?php
namespace app\lib
{
class Index
{
}
}
Namespaces should be written in lower case:
// Correct
namespace foo\bar;
// Incorrect
namespace Foo\Bar;
Class names should be written in upper CamelCase:
// Correct
class Image
// Correct
class MyImage
// Incorrect
class image
// Incorrect
class my_image
Method names should be written in lower camelCase (does not apply to controller and task methods):
// Correct
public function fooBar()
// Incorrect
public function FooBar()
// Incorrect
public function foo_bar()
Variable names should be written in lower camelCase:
// Correct
$fooBar = null;
// Incorrect
$foobar = null;
// Incorrect
$foo_bar = null;
Constant names should be written in upper case and multiple words should be separated with a underscore:
// Correct
const FOO_BAR = 123;
// Correct
define('FOO_BAR', 123);
// Incorrect
const foobar = 123;
// Incorrect
define('foobar', 123);
Braces associated with a control statement should always be on the next line, indented to the same level as the control statement:
// Correct
public function foo()
{
}
// Incorrect
public function foo() {
}
Tabs should be used for indentation while spaces should be used to align code:
<?php
namespace foo;
class Bar
{
public function hello()
{
$string = 'Hello ';
$string .= 'World!';
echo $string;
}
}
All classes, methods and functions are commented using the PHPDoc standard.
This makes it easy to understand what the code does and it also enables IDEs to provide improved code completion, type hinting and debugging.
/**
* Returns a greeting.
*
* @access public
* @param string $name Name of the person you want to greet
* @return string
*/
public function greeting($name)
{
return 'Hello, ' . $name . '!';
}
- Getting started: Installation
- Getting started: Configuration
- Getting started: Error handling
- Getting started: Constants
- Getting started: Coding standards
- Controllers: Controllers
- Controllers: Request
- Controllers: Response
- Databases: Basics
- Databases: Query builder
- Databases: Orm
- Databases: Migrations
- Command line: Basics
- Command line: Custom tasks
- Packages: Basics
- Packages: Composer packages
- Learn more: Array helper
- Learn more: Asset manager
- Learn more: Autoloading
- Learn more: Caching
- Learn more: Cookies
- Learn more: Events
- Learn more: File helper
- Learn more: Html helper
- Learn more: Internationalization
- Learn more: Logging
- Learn more: Number helper
- Learn more: Pagination
- Learn more: Redis client
- Learn more: Rest client
- Learn more: Security helpers
- Learn more: Sessions
- Learn more: String helper
- Learn more: Url helper
- Learn more: Uuid helper
- Learn more: Validation
- Learn more: Views