PHP standards

What is PSR? PSR is a standard established by PHP-FIG(PHP Framework Interop Group). PSR consists of several standards such as PSR-1, PSR-2 and PSR-4. PSR-0 and PSR-4 are coding standards for PHP’s autoloaders, PSR-1 and PSR-2 are basic coding standards. PHP code MUST use only UTF-8 without BOM(Byte Order Mark). PSR-1 Basic Coding Standard, defines basic parts of PHP coding Only <?php ?> or <?= ?> are allowed for PHP tags Files must be in UTF-8 without BOM(Byte Order Mark) Namespaces and class names must follow the standards in PSR-0 and PSR-4 Class names must be defined in UpperCamelCase Class variables must be defined in UPPER_SNAKE_CASE Method names must be defined in camelCase // defining normal properties in camelCase // defining static properties in UpperCamelCase // Class constants MUST be declared in all upper case with underscore separators class Something { public $normalPropterty; public static $StaticProperty; const VERSION = '1.0'; const DATE_APPROVED = '2000-12-01'; } PSR-2 Coding style Guide, an extension of PSR-1 You must follow PSR-1 coding standards 4 spaces must be used for indents. Using tabs is not allowed There is no limit to line length, but it should be under 120 characters, and best if under 80 You must put a newline before curly braces for classes and methods Methods and properties must be defined with abstract/final first, followed with public/protected, and finally static. You must not put a newline before curly braces in conditional statements You must not put any spaces before ( and ) in conditional statements PSR-12: Extended Coding Style, is now recommended for PSR-2. .php files must use Unix LF(linefeed) line \n ending only. .php files must end with a non-blank line. closing ?> tag must be omitted from .php files containing only php. Compound namespaces with a depth of more than two MUST NOT be used. namespace SubnamespaceOne\Anothernamespace\ClassA; <?php /** * This file contains an example of coding styles. */ declare(strict_types=1); // MUST be one blank line after the namespace declaration namespace Vendor\Package; // MUST be one blank line after the block of use declarations. use Vendor\Package\{ClassA as A, ClassB, ClassC as C}; use Vendor\Package\SomeNamespace\ClassD as D; use function Vendor\Package\{functionA, functionB, functionC}; use const Vendor\Package\{ConstantA, ConstantB, ConstantC}; use Vendor\Package\FirstTrait; // Opening braces for classes MUST go on the next line, and closing braces MUST go on the next line after the body. class Foo extends Bar implements FooInterface { // use keyword used inside the classes to implement traits MUST be declared on the next line after the opening brace. use FirstTrait; // Opening braces for methods MUST go on the next line, and closing braces MUST go on the next line after the body. public function sampleFunction(int $a, int $b = null): array { if ($a === $b) { bar(); } elseif ($a > $b) { $foo->bar($arg1); } else { BazClass::bar($arg2, $arg3); } } // abstract and final MUST be declared before the visibility; // static MUST be declared after the visibility. final public static function bar() { // method body } } Reference PSR-1 PSR-2 PSR-12 5 PHP coding statands Code Sytle Guide

June 17, 2020 · 3 min · 513 words · Ken Cho

Learn, todo and done 15-19/6/20

Learn Docker TDD PHP standard PHPUnit Codeception Yii1.1 and Yii2 Todo Issue #423 Issue #440 Issue #425 Problem Done Created PR for issue #291 PHPUnit doc, YouTube PHP Standards: PSR-1, PSR-2 and PSR-12 Reference

June 15, 2020 · 1 min · 34 words · Ken Cho

Learn, todo and done 8-12/6/20

Learn Docker PostgreSQL OOP progress Yii1.1 framework utterances Todo Problem For Issue #291, the codes in local develop branch is huge different from that in production server, a cascade of changes will be needed. Yii1.1 blog update Home page from /site/index to /site/post in /layouts/main.php Done Fixed issue #421 Fixed issue #441 Issue #291 Reference How to add Utterances in Hugo post utterances

June 8, 2020 · 1 min · 63 words · Ken Cho

Yii1.1 for beginners

What is Yii Model-View-Controller architecture? MVC schema Model defines relations among data in DB and rules that must be followed when saving data to DB. It also gives us tools to read/save data from/to DB. View shows data to user. It doesn’t write to DB or count difficult things. It just receives data and shows them using HTML, CSS, JS, etc. Controller processes and changes the data, handles user’s actions, decides, counts, thinks and calls models and views. It simply acts. ...

June 4, 2020 · 3 min · 532 words · Ken Cho

How to build a blog using Yii1.1

Aim To familiarize the framework of Yii 1.1. How The steps can be found here The source code is stored at github repo Docker container for Nginx and PHP=FPM webserver, link Steps to note Download the Yii 1.1 source code at here Unpack the compressed file and move to dockerfolder/app tar -xvzf yii-1.1.22.bf1d26.tar.gz Rename the folder name to yii-1.1.22 Go to the framework folder to create a skeleton application by ./yiic webapp ../blog-yii1.1 Establish DB connection in /protected/config/database.php return array( 'connectionString' => 'sqlite:'.dirname(__FILE__).'/../data/blog.db', ); Allowing Gii for scafolding /protected/config/main.php 'import'=>array( 'application.models.*', 'application.components.*', ), 'modules'=>array( 'gii'=>array( 'class'=>'system.gii.GiiModule', 'password'=>'Password', 'ipFilters' = False, ), ), How to customize the rules method in /protected/models/Post.php public function normalizeTags($attribute,$params) { $this->tags=Tag::array2string(array_unique(Tag::string2array($this->tags))); } public function rules() { return array( array('title, content, status', 'required'), array('title', 'length', 'max'=>128), array('status', 'in', 'range'=>array(1,2,3)), array('tags', 'match', 'pattern'=>'/^[\w\s,]+$/', 'message'=>'Tags can only contain word characters.'), array('tags', 'normalizeTags'), array('title, status', 'safe', 'on'=>'search'), ); } How to customize the rules method in /protected/models/Tag.php public static function string2array($tags) { return preg_split('/\s*,\s*/',trim($tags),-1,PREG_SPLIT_NO_EMPTY); } public static function array2string($tags) { return implode(', ',$tags); } How to customize the relations method in protected/models/Post.php public function relations() { // NOTE: you may need to adjust the relation name and the related // class name for the relations automatically generated below. return array( 'author' => array(self::BELONGS_TO, 'User', 'author_id'), 'comments' => array(self::HAS_MANY, 'Comment', 'post_id', 'condition'=>'comments.status='.Comment::STATUS_APPROVED, 'order'=>'comments.create_time DESC'), 'commentCount' => array(self::STAT, 'Comment', 'post_id', 'condition'=>'status='.Comment::STATUS_APPROVED), ); } How to customize the relations method in protected/models/Comment.php class Comment extends CActiveRecord { const STATUS_PENDING=1; const STATUS_APPROVED=2; const STATUS_ARCHIVED=3 ...... How to create portlets for User Menu, Tag Cloud and Recent Comments 11.1 UserMenu portlet 11.2 TagCloud ...

June 1, 2020 · 2 min · 284 words · Ken Cho