每个函数执行的PHP操作

每个函数执行的PHP操作,php,oop,function,Php,Oop,Function,我有以下情况 我有一个有很多函数的类。每个函数都从执行相同的方法开始。是否有一种方法可以将此方法实现到函数中,以便自动执行 以下是一个例子: class test { static function_1($param) {some_method($param); other stuff....} static function_2($param) {some_method($param); other stuff then function 1....} static function

我有以下情况

我有一个有很多函数的类。每个函数都从执行相同的方法开始。是否有一种方法可以将此方法实现到函数中,以便自动执行

以下是一个例子:

class test
{
 static function_1($param) {some_method($param); other stuff....} 
 static function_2($param) {some_method($param); other stuff then function 1....} 
 static function_3($param) {some_method($param); other stuff then function 1 and function 2....} 
}
那么有没有办法执行
some_method()自动,而不在每个函数中声明它

提前谢谢

全部代码:

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
 * The Assets Library
 * 
 * This class let's you add assets (javascripts, stylesheets and images) way easier..
 */


class Assets {
    private $css_url;
    private $js_url;
    private $img_url;

    public function __construct()
    {
        $CI =& get_instance();
        $CI->config->load('assets');

        $asset_url = base_url() . $CI->config->item('assets_dir');
        $this->css_url = $asset_url . $CI->config->item('css_dir_name');
        $this->js_url = $asset_url . $CI->config->item('js_dir_name');
        $this->img_url = $asset_url . $CI->config->item('img_dir_name');
    }

    // Returns the css html link
    public function css_html_link($filename)
    {
        // Check whether or not a filetype was given
        $filename = $this->_add_filetype($filename, 'css');

        $link = '<link type="text/css" rel="stylesheet" href="' . $this->css_url . $filename . '" />';
        return $link;
    }

    // Returns the css link
    public function css_link($filename)
    {
        $filename = $this->_add_filetype($filename, 'css');

        return $this->css_url . $filename;
    }

    // Returns the js html link
    public function js_html_link($filename)
    {
        $filename = $this->_add_filetype($filename, 'js');

        $script = '<script type="text/javascript" src="' . $this->js_url . $filename . '"></script>';
        return $script;
    }

    // Return the js link
    public function js_link($filename)
    {
        $filename = $this->_add_filetype($filename, 'js');

        return $this->js_url . $filename;
    }

    // Returns the image html tag
    public function img_html_link($filename, $rel = NULL)
    {
        // Get the filename without the filetype
        $alt_text = substr($filename, 0, strpos($filename, '.')+1);
        $alt_text = 'alt="'.$alt_text.'"';

        // If relation is giving, use it
        $img_rel = ($rel !== FALSE) ? 'rel="' . $rel . '"' : '';

        $image = '<img src="' . $this->img_url . $filename . '" '.$rel.' ' . $alt_text . '/>';
        return $image;
    }

    // Return the image link
    public function img_link($filename)
    {
        return $this->img_url . $filename;
    }

    // Check whether or not a filetype was specified in $file, if not, it will be added
    private function _add_filetype($file, $type)
    {
        if(strpos($file, '.' . $type) === FALSE)
        {
            $file = $file . '.' . $type;
        }
        return $file;
    }
}

/* End of file assets.php */
/* Location: ./application/libraries/assets.php */

每次初始化类时,它都会调用_construct()函数,或者在PHP4中(我希望您没有使用PHP4),它使用与类同名的函数

如果您这样做,它应该适用于该课程的每个学员:

function __construct($param){
    some_method($param);
}
如果在类的同一初始化中调用多个函数,则可以执行以下操作:

var $param;

function __construct($param){
    $this->param = $param;
}

function doMethod(){
    some_method($this->param);
}

function function_1()
{
    $this->doMethod();
}
使用不同的参数多次调用该类。或许可以尝试这种方法:

function __call($function, $param){
    some_method($param);
    switch ($function){
         case 'function1':
         $this->function1($param);
         break;
         /// etc..
    }
}

每次初始化该类时,它都会调用_construct()函数,或者在PHP4中(我希望您没有使用PHP4),它会使用与该类同名的函数

如果您这样做,它应该适用于该课程的每个学员:

function __construct($param){
    some_method($param);
}
如果在类的同一初始化中调用多个函数,则可以执行以下操作:

var $param;

function __construct($param){
    $this->param = $param;
}

function doMethod(){
    some_method($this->param);
}

function function_1()
{
    $this->doMethod();
}
使用不同的参数多次调用该类。或许可以尝试这种方法:

function __call($function, $param){
    some_method($param);
    switch ($function){
         case 'function1':
         $this->function1($param);
         break;
         /// etc..
    }
}

您可以创建一个包含类测试(组合)实例的类,并实现其神奇方法。类似于:

class testWrapper
{
    private $test;

    function __construct()
    {
        $this->test = new Test();
    }

    function __call($name, $args)
    {
        call_user_func_array(array($this->test, 'some_method'), $args);
        call_user_func_array(array($this->test, $name), $args);
    }
}
然后从testWrapper实例对象上的
test
类调用方法。
您可以进一步细化
\u call
方法中的逻辑,以便根据传入的方法名称等仅调用
某些方法()

您可以创建一个包含类测试(组合)实例的类,并实现其神奇方法。类似于:

class testWrapper
{
    private $test;

    function __construct()
    {
        $this->test = new Test();
    }

    function __call($name, $args)
    {
        call_user_func_array(array($this->test, 'some_method'), $args);
        call_user_func_array(array($this->test, $name), $args);
    }
}
然后从testWrapper实例对象上的
test
类调用方法。
您可以进一步细化
\u call
方法中的逻辑,根据传入的方法名称等,仅调用
某些方法()

在这种情况下,恐怕答案是“否”

您不是在“声明”某个方法()
每次都在调用它。如果你不调用它,它就不能运行,所以你每次都要调用它

剪贴

为什么不把你的实际代码粘贴在这里呢,一些重构可能会有所帮助

查看实际代码后编辑


我看不出你现有代码的问题。这很清楚,一年后你就会知道它会做什么。我会保持原样。您接受的答案会起作用,但会混淆您的代码。将来回来维护代码时,您将很难弄清楚自己做了什么以及为什么要这么做。

恐怕在这种情况下,答案是“否”

您不是在“声明”某个方法()
每次都在调用它。如果你不调用它,它就不能运行,所以你每次都要调用它

剪贴

为什么不把你的实际代码粘贴在这里呢,一些重构可能会有所帮助

查看实际代码后编辑


我看不出你现有代码的问题。这很清楚,一年后你就会知道它会做什么。我会保持原样。您接受的答案会起作用,但会混淆您的代码。当您将来回来维护代码时,您将很难弄清楚自己做了什么以及为什么要这样做。

@Dagon:那么,当对象实例化时,它只会运行一次。我认为每次在对象中调用一个方法时,op都想做一些内务处理,随着对象状态的变化,每次调用的运行方式可能会有所不同。@Dagon:那么,当对象实例化时,它只运行一次。我认为op希望每次在对象中调用一个方法时都进行一些内务处理,随着对象状态的变化,每次运行的方式可能会有所不同。对象只会初始化一次,但函数会被多次使用,并且$param的值不同,因此这恐怕不起作用。我刚刚添加了第三个选项,您可以使用该选项。对象只会初始化一次,但函数会被多次使用如果$param有不同的值,那么这恐怕就行不通了。我刚刚添加了第三个选项,您可以使用它。您可以举个例子说明如何实现它吗?这种方法能在CodeIgniter框架内工作吗?因为我正在使用它。你能举个例子说明如何实现它吗?这种方法能在CodeIgniter框架内工作吗?因为我正在使用它。好的,我在这里复制了实际的代码,你可以看一下吗?我指的方法是_add _filetypeOk我在这里复制了实际的代码,代码你可以看一下吗?我指的方法是_add _filetype