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

PHPUnit

What is PHPUnit? The goal of unit testing is to isolate each part of the programme and show that the individual parts are correct. What does Assertions mean? Just asserting that something match the expected value or does something you wanted to do. Whats the flow of running phpunit? Install the PHPUnit Configure the phpunit.xml <?xml version="1.0" encoding="utf-8" ?> <phpunit bootstrap="vendor/autoload.php" colors="true" verbose="true" stopOnFailure="false"> <testsuites> <testsuite name="unit"> <directory>tests/unit</directory> </testsuite> </testsuites> </phpunit> Create app, app/Modelsand tests/unit folders Build codes bite by bite, and then run phpunit ./vendor/bin/phpunit File name and the class name must be the same! For example, UserTest.php ...

June 16, 2020 · 2 min · 341 words · Ken Cho

Docker 101

What is Docker? In simpler words, Docker is a tool that allows developers, sys-admins etc. to easily deploy their applications in a sandbox (called containers) to run on the host operating system i.e. Linux. The key benefit of Docker is that it allows users to package an application with all of its dependencies into a standardized unit for software development. Unlike virtual machines, containers do not have high overhead and hence enable more efficient usage of the underlying system and resources. ...

June 11, 2020 · 4 min · 704 words · Ken Cho

What is Tdd?

What is TDD? Before writing any code that adds new functionality to an application, the developer first writes an automated test describing how the new code should behave, and watches it turn red (fail to pass). They then write the code to the specification, and the test turns green (it passes). Finally, the developer takes a little time to make sure that the code just written is as clean as possible (refactoring). ...

June 10, 2020 · 4 min · 680 words · Ken Cho

Postgresql

What is PostgreSQL? PostgreSQL is an enterprise-class open source database management system. It supports both SQL for relational and JSON for non-relational queries. PostgreSQL supports advanced data types and advance performance optimization. Key features Compatible with various platforms using all major languages and middleware It offers a most sophisticated locking mechanism Support for multi-version concurrency control Compliant with the ANSI SQL standard Full support for client-server network architecture Log-based and trigger-based replication SSL Standby server and high availability Object-oriented and ANSI-SQL2008 compatible Support for JSON allows linking with other data stores like NoSQL which act as a federated hub for polyglot databases MySQL vs PostgreSQL MySQL PostgreSQL The MySQL project has made its source code available under the terms of the GNU License, and other proprietary agreements. PostgreSQL is released under PostgreSQL License. It’s now owned by Oracle Corporation and offers several paid editions. It’s free and open-source software. That means you will never need to pay anything for this service. MySQL is ACID compliant only when using with NDB and InnoDB Cluster Storage engines. PostgreSQL is completely ACID compliant. MySQL performs well in OLAP and OLTP systems where only read speed is important. PostgreSQL performance works best in systems which demand the execution of complex queries. MySQL is reliable and works well with BI (Business Intelligence) applications, which are difficult to read PostgreSQL works well with BI applications. However, it is more suited for Data Warehousing and data analysis applications which need fast read-write speeds. Advantages Can run dynamic websites and web apps. Write-ahead logging makes it a highly fault-tolerant database. Source code is freely available under an open source license. Supports geographic objects so you can use it for location-based services and geographic information systems. Supports geographic objects so it can be used as a geospatial data store for location-based services and geographic information systems. Low maintenance administration for both embedded and enterprise use. Disadvantages Changes made for speed improvement requires more work than MySQL as PostgreSQL focuses on compatibility. Many open source apps support MySQL, but may not support PostgreSQL. On performance metrics, it is slower than MySQL. Download and install Download and install postgrlSQL brew doctor brew update brew install postgresql Download and install Pgcli, for Postgres with auto-completion and syntax highlighting. brew install pgcli The path of psql brew info libpq To include in in zshrc echo 'export PATH="/usr/local/opt/libpq/bin:$PATH"' >> ~/.zshrc Postgre.app Postgre.app is a full-featured PostgreSQL installation packaged as a standard Mac app. ...

June 9, 2020 · 7 min · 1319 words · Ken Cho