Home > PHP Framework > Workerman > body text

Registration tree mode in Workerman

Release: 2019-12-27 17:26:39
forward
2488 people have browsed it

Registration tree mode in Workerman

The registration tree mode is to hang the object into the attribute array of a class, and fetch it directly from this array next time, keeping it globally unique. It is generally useful when initializing the project entrance. The very beginning of Workerman is the application of the registration tree mode. The following is its simulation:

<?php
class Worker{

    protected static $_workers=array();
    public function __construct()
    {
        $this->workerId=spl_object_hash($this);
        static::$_workers[$this->workerId]=$this;
    }
    public static function runAll(){
        foreach (static::$_workers as $worker) {
            var_dump($worker);
        }
    }
}

new Worker();
new Worker();
Worker::runAll();
Copy after login

In the constructor of Worker, the current new object is hung into the static variable attribute array of the Worker class. , take

Registration tree mode in Workerman

directly from that array the next time you use it. For more workerman knowledge, please pay attention to the workerman tutorial column.

The above is the detailed content of Registration tree mode in Workerman. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:cnblogs.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!