SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    Halifax
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Running dynamic method with variable parameter length

    Ok here is my problem.

    At the moment I have an object which declares a set of rules to be applied to another object.

    However I'm unsure to apply dynamically call the method along with a variable number of parameters, other than using eval.

    Anyone any ideas:

    Code:
    //Example validation object
    class cardValidation {
    	
    	function validate($card_number) {
    	
    	}
    	
    	function validate1($start_date, $end_date) {
    		
    	}
    
    }
    
    //Example card object
    class card {
    	
    	var $card_number;
    	
    	function __construct() {
    		$this->card_number = 123456789101112;
    		$this->start_date = '2007-08-12';
    		$this->end_date = '2008-08-12';
    	}
    	
    }
    
    //Set up the objects
    $card_number = new cardValidation;
    $card = new card;
    
    $rules = new ReflectionClass($card_number);
    
    foreach($rules->getMethods() as $method) {
    
    		foreach($method->getParameters() as $p)	{
    			$param = $p->getName();	
    			$params[$param] = $card->$param;	
    		}	
    		
    		$method_name = $method->getName();
    
    }
    
    //Dynamically applying everything at the end?

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2003
    Location
    Halifax
    Posts
    191
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not the best etiquette replying to your own post.

    Think I've probably solved this by changing things round and using ReflectionClass::newInstanceArgs to apply an array to the construct of the object instead.

  3. #3
    SitePoint Evangelist
    Join Date
    Mar 2005
    Posts
    421
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes an array, or you could use func_get_arg(s)? Not as cleaner as reflection but probably a little simpler to understand.

  4. #4
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm pretty sure this is what you're looking for.

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Perhapses this would be of help.

    PHP Code:
    <?php

    class Test {

        static private 
    $_store;

        public function 
    dyn () {

            
    $this->_store = (object)null;

            
    $num  func_get_args();
            
    $args func_get_args();

            if (
    $num == && is_array($args[0])) {
                
                foreach (
    $args as $k => $v) {
                    
    $this->_store->{$k} = $v;
                }

            } else {

                for (
    $i 0$i $num$i += 2) {
                    
    $this->_store->{$args[$i]} = $args[$i 1];
                }

            }

        }

        public function 
    __get ($var) {

            if (isset(
    $this->_store->{$var})) {
                return 
    $this->_store->{$var};
            }

        }

    }

    $test = new Test();

    # Strided list.
    $test->dyn('Itemname''Itemvalue''card_number'79535'card_date''12-12-2007');

    # Array.
    $this->dyn(array('Itemname' => 'Itemvalue''card_number' => 79535'card_date' => '12-12-2007'));
    Last edited by logic_earth; Aug 18, 2007 at 06:19.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •