Development Specifications

Application Directory

The application directory can be placed anywhere.

Entry File

Similar to PHP applications under nginx+PHP-FPM, applications in Workerman also require an entry file. There are no specific naming requirements for the entry file, and this entry file is run in PHP CLI mode.

The entry file contains code related to creating listening processes, for example, the following code snippet based on Worker:

test.php

<?php
use Workerman\Worker;
use Workerman\Connection\TcpConnection;
require_once __DIR__ . '/vendor/autoload.php';

// Create a Worker to listen on port 2345 and communicate using the http protocol
$http_worker = new Worker("http://0.0.0.0:2345");

// Start 4 processes to provide services to the outside
$http_worker->count = 4;

// Reply 'hello world' to the browser when data is received from it
$http_worker->onMessage = function(TcpConnection $connection, $data)
{
    // Send 'hello world' to the browser
    $connection->send('hello world');
};

Worker::runAll();

Code Specifications in Workerman

  1. Classes should use CamelCase naming with the first letter capitalized. The class file name must match the class name inside the file for autoloading purposes. For example:

    class UserInfo
    {
    ...
  2. Use namespaces, with the namespace name corresponding to the directory path, based on the developer's project root directory.

For example, in the project MyApp/, the class file MyApp/MyClass.php does not require a namespace because it is in the project root. However, for the class file MyApp/Protocols/MyProtocol.php, since MyProtocol.php is in the Protocols directory of the MyApp project, it should include the namespace namespace Protocols;, as follows:

namespace Protocols;
class MyProtocol
{
....
  1. Ordinary functions and variable names should use lowercase with underscores, for example:

    $connection_list = array();
    function get_connection_list()
    {
    ....
  2. Class members and methods should use camelCase with the first letter lowercase, for example:

    public $connectionList;
    public function getConnectionList();
  3. Function and class parameters should use lowercase with underscores:

    function get_connection_list($one_param, $two_param)
    {
    ....