Yii framework implements custom third-party extension operation example

Time:2020-1-14

The example of this paper describes the implementation of custom third-party extension operation in Yii framework. To share with you for your reference, as follows:

cat.php

<?php
/**
 * Created by PhpStorm.
 * Date: 2016/5/25
 * Time: 15:23
 */
namespace vendor\animal;
Use \ Yii \ base \ component; // component
use \yii\base\Event;//
class myEvent extends Event{
  public $message;
}
class Cat extends Component{
  public function shout(){
    echo "miao miao miao<br>";
    //$this - > trigger ('miao '); // event name
    $me = new myEvent();
    $me->message = "hello my Event<br>";
    $this->trigger('miao',$me);
  }
}

Dog.php

<?php
/**
 * Created by PhpStorm.
 * Date: 2016/5/25
 * Time: 17:00
 */
namespace vendor\animal;
use frontend\behaviors\behavior1;
Use Yii \ base \ component; // let a class have the ability to receive behavior
class Dog extends Component{
  public function behaviors(){
    return [
      Behavior1::className(),
    ];
  }
  public function look()
  {
    echo "I am looking!<br>";
  }
}

Mouse.php


<?php
/**
 * Created by PhpStorm.
 * Date: 2016/5/25
 * Time: 15:23
 */
namespace vendor\animal;
class Mouse{
  public function run($e)
  {
    echo $e->message;
    echo "I am running!<br>";
  }
}

controllers/AnimalController.phpCall third-party class library

<?php
/**
 * Created by PhpStorm.
 * Date: 2016/5/25
 * Time: 15:27
 */
//Yii event binding
namespace frontend\controllers;
use yii;
use yii\web\Controller;
use vendor\animal\Cat;
use vendor\animal\Mouse;
use vendor\animal\Dog;
Use \ Yii \ base \ event; // class level event binding must
Use frontend \ behaviors \ behavior1; // mixing of objects
class AnimalController extends Controller{
  public function actionIndex(){
    $cat = new Cat();
    $mouse = new Mouse();
    $dog = new Dog();
    //Object level event binding
    $cat->on('miao',[$mouse,'run']);
    $cat->on('miao',[$dog,'look']);
//
//$cat - > off ('miao ', [$dog,' look ']; // cancel the attention
    //Class level event binding
//Event:: on (cat:: classname(),'miao ', [$mouse,' run ']); // all mice will listen to this Miao event
//    Event::on(Cat::className(),'miao',function(){
//      echo "miao event has triggerd!<br>";
//    });
    $cat->shout();
    //\Yii:: $app is a singleton pattern implemented by Yii
//    Yii::$app->on(\yii\base\Application::EVENT_AFTER_REQUEST,function(){
//      echo "event after request";
//    });
  }
  Public function actionbehavior() // mixing of classes
  {
    * * plate 1
     * $dog = new Dog();
    $dog->look();
    $dog->eat();*/
    $dog=new Dog();
    $dog->trigger("wang");
  }
  Public function actionobject() // mixing of objects
  {
    $dog = new Dog();
    $behavior1 = new Behavior1();
    $dog - > attachbehavior ('beh1 ', $behavior1); // take an alias to prevent conflicts
    //$dog - > detachbehavior ('beh1 '); // remove the object
    $dog->eat();
  }
}

For more information about the content of Yii, please refer to the following topics: introduction to Yii framework and summary of common skills, summary of PHP excellent development framework, basic course of smart template introduction, introduction to PHP object-oriented programming, summary of PHP string usage, introduction to PHP + MySQL database operation and PHP common database operation Skills summary

I hope that this article will be helpful for you to Design PHP program based on Yii framework.

Recommended Today

The basic syntax and function of triggers in SQLSEVER

What is a trigger? Triggers are stored procedures that are executed automatically when a table is inserted, updated, or deleted. A special stored procedure that is automatically executed when a trigger inserts, updates, or deletes a table. Triggers are generally used for more complex check constraints. The difference between a trigger and a normal stored […]