【问题标题】:PHP Class: writing functions inside an object?PHP 类:在对象内编写函数?
【发布时间】:2013-08-03 22:05:09
【问题描述】:

我想知道这在 php 类对象中是否可能,就像我在 javascript (jquery) 中所做的那样。

在 jquery 中,我会这样做,

(function($){


    var methods = {

    init : function( options ) {
       // I write the function here...  

    },

    hello : function( options ) {

           // I write the function here...  
        }
     }

    $.fn.myplugin = function( method ) {

    if ( methods[method] ) {
            return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
        } else if ( typeof method === 'object' || ! method ) {
            return methods.init.apply( this, arguments );  
        } else {
            $.error( 'Method ' +  method + ' does not exist.' );
        }
        return this; 
    };
})(jQuery);

所以,当我想调用myplugin 中的函数时,我就这样做了,

$.fn.myplugin("hello");

所以,我想,当你来写一个类时,可能有一种方法可以在 php 中做到这一点?

$method = (object)array(
"init" => function() { 
   // I write the function here...   
}, 
"hello" => function() { 
 // I write the function here...    

} 
);

编辑:

会是这样的课程吗?

class ClassName {

    public function __construct(){
    //
    }

    public function method_1(){

        $method = (object)array(

            "init" => function() { 
               // I write the function here...   
            }, 
            "hello" => function() { 
             // I write the function here...    

            } 
        );

    }


    public function method_2(){

        $method = (object)array(

            "init" => function() { 
               // I write the function here...   
            }, 
            "hello" => function() { 
             // I write the function here...    

            } 
        );

    }

}

【问题讨论】:

    标签: php oop stdclass php-5.4


    【解决方案1】:

    您的$.fn.myplugin 函数与PHP 中的__call() 魔术函数非常相似。但是,您必须在一个类中定义它并模拟逻辑:

    class Example {
        private $methods;
    
        public function __construct() {
            $methods = array();
            $methods['init'] = function() {};
            $methods['hello'] = function() {};
        }
    
        public function __call($name, $arguments) {
            if( isset( $methods[$name])) {
                call_user_func_array( $methods[$name], $arguments);
            } else if( $arguments[0] instanceof Closure) {
                // We were passed an anonymous function, I actually don't think this is possible, you'd have to pass it in as an argument
                call_user_func_array( $methods['init'], $arguments);
            } else {
                throw new Exception( "Method " . $name . " does not exist");
            }
        }
    }
    

    那么,你会这样做:

    $obj = new Example();
    $obj->hello();
    

    它没有经过测试,但希望这是一个开始。

    【讨论】:

    • 谢谢,但他们不应该是$obj = new Example();public function Example() {}吗??
    • @lauthiamkok - 因此“它没有经过测试”......我会做一些小的改变。
    【解决方案2】:

    PHP 支持闭包(匿名函数) 类似jQuery的看看

    function x(callable $c){
         $c();
    }
    

    然后使用

    x(function(){
         echo 'Hello World';
    });
    

    【讨论】:

      【解决方案3】:
      class ClassName {
      
          public function __construct(){
          //this is your init
          }
      
          public function hello(){
          //write your function here
          }
      
      }
      

      你会怎么写

      然后

      $a =  new ClassName()
      
      $a->hello();
      

      叫它

      【讨论】:

      • 你想要一个匿名类?
      • 也许吧,但什么是匿名类?以前从未这样做过。匿名类有什么好处??
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      • 2022-11-05
      • 1970-01-01
      相关资源
      最近更新 更多