【问题标题】:Dynamic method name inside a class?类中的动态方法名称?
【发布时间】:2017-03-29 08:20:15
【问题描述】:

我想,在实例化一个类以便能够传递给它一个名称时,该名称将用于为该类中的方法名称添加前缀,到目前为止我有这个

<?php
if ( ! class_exists( 'My_Class' ) ) {
    /**
     * Creates filter method
     *
     * @since  1.0.0
     */
    class My_Class {

        /**
         * Prefix for method names
         *
         * @var string $name Name of the filter prefix.
         */
        private $name;

        /**
         * Version.
         *
         * @var string
         */
        private $version = '1.0.0';

        /**
         * Initialize the class and set its properties.
         *
         * @since 1.0.0
         * @param string $name    The name for the method prefix.
         * @param string $version The version of this plugin.
         */
        public function __construct( $name, $version ) {
            $this->name = $name;
            $this->version = $version;
            add_filter( 'my_filter_name', $name . '_my_folder' );
        }

        /**
         * Function for filtering folder array
         *
         * Each plugin has to set its own array of paths and url.
         *
         * @param  array $import_array Array with folder values.
         * @return array               Modified array with folder values.
         */
        public function $name . _my_folder() {
            $import_array[$this->name]['folder']     = plugin_dir_path( __FILE__ ) . 'includes/layout';
            $import_array[$this->name]['folder_url'] = plugin_dir_url( __FILE__ ) . 'includes/layout';
            return $import_array;
        }

    }
}

我的想法是这样称呼它

$first_object = new My_Class( 'first' );
$second_object = new My_Class( 'second' );

这样我就可以在多个相同的插件中使用它,但名称不同,具体取决于插件的类型。

明显的问题是function $name . _my_folder()

我读过一些关于 __call() 魔法方法的文章,但我不确定这是否可以在这种情况下使用,或者如何应用它。

这个可以吗?

【问题讨论】:

  • 为什么需要这个?像这样使用它绝对是个坏主意。这似乎是糟糕的 OOP 设计。请描述您的用例。
  • 在 WordPress 中,如果函数中包含 apply_filters 函数,您可以使用过滤器来修改函数的工作方式。我需要能够附加到现有数组,包含所有插件 url 的数组(外观相同的插件,具有不同的导入文件)。为此,我可以手动编写函数并将它们添加到过滤器,如果有很多这样的插件,这是一项漫长而乏味的任务,所以我的想法是创建一个可以为我执行此操作的对象,因为只有函数的前缀名称用于过滤器更改(和数组键)

标签: php wordpress oop methods


【解决方案1】:

你为什么不把方法名作为参数呢?

像这样:

add_filter('my_filter_name', [$this, 'methodName'], 10, 1);

第三个参数是过滤器的优先级,第四个是接受的参数个数。

public function methodName($name) {}

然后这样称呼它:

apply_filters('my_filter_name', $value, $arg);

【讨论】:

  • 我会看看这是否可行。理论上应该,我以前做过,不知道为什么我一开始没有使用它。谢谢
  • 否则你可以使用魔术__call($name, $args)方法但是参数是更好的解决方案。
  • 通过一些修改可以工作,但plugin_dir_path( __FILE__ ) 总是指向两个插件之一。它注册了两个插件,并且数组中填充了 2 个很棒的数组(名称在数组中正确传递),但是两种情况下的路径都是相同的...
  • 这是有道理的,因为我在第一个插件中定义了类,所以它用于在第二种情况下创建一个具有相同 dir 路径的实例......我必须将此作为参数添加为出色地。我很笨xD
  • 没错,将文件路径作为下一个参数传递
【解决方案2】:

我不是专家,但我认为您必须在构造函数中指定默认版本...我不确定,但您可以尝试一下。

    public function __construct( $name, $version="1.0.0" ) {
        $this->name = $name;
        $this->version = $version;
        add_filter( 'my_filter_name', $name . '_my_folder' );
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-03-21
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    • 2011-08-26
    • 1970-01-01
    • 2020-08-10
    • 2018-12-15
    相关资源
    最近更新 更多