Home > Backend Development > PHP Tutorial > How to use magic methods to implement cross-file calling functions through the Yii framework

How to use magic methods to implement cross-file calling functions through the Yii framework

不言
Release: 2023-04-01 09:04:02
Original
1558 people have browsed it

This article mainly introduces the Yii framework's use of magic methods to implement cross-file calling functions, involving operating techniques related to PHP object-oriented programming in the Yii framework. Friends in need can refer to the following

The examples in this article describe Yii The framework uses magic methods to implement cross-file calls. Share it with everyone for your reference, the details are as follows:

The current project uses the Yii framework, the controller calls the facade method, the facade calls the adapter method, the adapter calls the api method, the api encapsulates the sql method, but in most cases Next, it is just a simple call, but limited to the rules of the current project, methods must be written, and the methods are all simple returns, so I wrote a demo and simulated it.

<?php
class aApi
{
  public static function tt1($name, $age)
  {
    print_r($name);
    echo $age;
  }
}
class aAdapter
{
  public function __call($func, $params)
  {
    $class = substr(get_called_class(), 0, -7) . &#39;Api&#39;;
    return call_user_func_array(array($class, $func), $params);
  }
}
class aFacade
{
  public static function __callstatic($func, $params)
  {
    // 这里也可以用debug_backtrace()
    $class = substr(get_called_class(), 0, -6) . &#39;Adapter&#39;;
    $obj = new $class();
    return call_user_func_array(array($obj, $func), $params);
  }
}
class aController
{
  public function actionC()
  {
    aFacade::tt1([&#39;name&#39;], &#39;age&#39;);
  }
}
$a = new aController;
$a->actionC();
Copy after login

The above is the entire content of this article. I hope it will be helpful to everyone’s study. For more related content, please pay attention to the PHP Chinese website !

Related recommendations:

How to implement the Yii framework to output and execute sql statements on the page and debug

How to implement the use of PHPExcel in the Yii2 framework Export Excel file

The above is the detailed content of How to use magic methods to implement cross-file calling functions through the Yii framework. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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