Learn php chat

    About communication.


    All people are independent (or almost)! All people communicate!
    Everyone knows the rite of communication between people, its causes. To get the information we need about her, you can ask, and not necessarily a specific person. Ask, so to speak, in the air, if someone knows the answer - he will answer.

    About JavaScript and Qt.


    I love javascript. I like its flexibility. I like his event system.
    Although I don't consider myself a C ++ programmer, I like Qt. His (her?) System of signals / slots.

    Why did I remember the language and the library? Because they can communicate, it sets them apart.

    About the idea.


    All modules are independent (or almost)! All modules communicate!
    Once I thought: why should I learn to communicate another favorite language of mine? PHP
    I imagined a system where all its components are independent of each other, moreover, they do not know about each other.
    They communicate.
     - Does anyone have the value of the variable xxx?
     - Yes, of course, hold it.
     - Can someone give me the last 10 comments?
     - No problem.


    On the implementation.


    After looking a bit for information on this issue, I found an attempt to implement Qt in PHP - QPHP, but this is not what I wanted. There is also an implementation of signals / slots in the ezComponents framework, but this is also not the case. Therefore, I started designing the core of the future library.
    In my imagination, it looked like this:
    image
    As you can see in the diagram, all components of the system depend only on the kernel, since all messages / events go through it.
    What I decided to include in the kernel:
    • the most important thing is the class-singleton that implements the communication itself, it imitates the basic Qt methods (connect, disconnet, emit, isConnected), and implements an exception system (messages to which the kernel does not need to respond)
    • signals and slots, it was decided to wrap in classes, for type control, and indeed encapsulation of some logic
    • the configurator is the class that loads the config, I decided to include it in the kernel ... This is a moot point, but it seemed right to me (of course, it will only communicate with modules with messages)
    • superclass for classes that will be used by messages (it makes the message manager request transparent)
      superclass for modules - it can load information about a module, check dependencies, etc.
    In my opinion this is quite enough.

    Implementation code is coming soon.

    About use


    Typical use, I see something like the following:
    1.  
    2. class module_test_1 extends module
    3. {
    4.     publicfunction __construct()
    5.     {
    6.         // согласен что выглядет немного мострообразно, но это для понимания, да и хуков никто не отменял
    7.         $this->emit(new signal('config.getvar', array('var' => 'varname', 'callback' => array($this, 'write')));
    8.         $this->emit(new signal('module_test_1.run'));
    9.     }
    10.    
    11.     publicfunction write($val)
    12.     {
    13.         echo "varname = $val";
    14.     }
    15. }
    16.  
    17. class module_test_2 extends module
    18. {
    19.     publicfunction __construct()
    20.     {
    21.         $this->connect(new slot('module_test_1.run', array($this, 'write')));
    22.     }
    23.    
    24.     publicfunction write()
    25.     {
    26.         echo "module_test_1 -> run";
    27.     }
    28. }
    29.  
    30. $test_2 = new module_test_2();
    31. $test_1 = new module_test_1();


    ______________________
    The text is prepared in the Habr Editor from © SoftCoder.ru

    Also popular now: