PHP fluent interface check if method exists

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • smartic
    New Member
    • May 2007
    • 150

    PHP fluent interface check if method exists

    i need to check if there is a method exists on my class or not before i call it here is my code :

    Code:
    <?php
    class elements{
    	function add($t){
    		if(class_exists($t)){
    			$c = new $t;
    			return $c;
    		}else{
    			echo 'class dos\'not exists';
    		}
    	}
    }
    class qw{
    	function render(){
    		echo 'ahmed saber amin';
    	}
    }
    $e = new elements();
    $e->add('qw')->renders();
    ?>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    to check for a method either use method_exists() or check against an interface.

    Comment

    • smartic
      New Member
      • May 2007
      • 150

      #3
      in my case where i would check in my code ?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        at latest just before you call the method, but it depends on the real code and what you want to do there.

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Also - go PHP5 :)

          Comment

          • smartic
            New Member
            • May 2007
            • 150

            #6
            anopther question is there away to declare dynamic property ex :

            Code:
            <?php
            class a{
            	function add($pro){
            		$this->$pro= 'ex@ex.com';
            	}
            }
            class e extends a{
            	function __constract(){
            		$this->add('email');
            	}
            }
            
            $e = new e();
            
            echo $e->email;
            
            ?>

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              not as a real property, but you can use __get() to work around that.

              Comment

              • Markus
                Recognized Expert Expert
                • Jun 2007
                • 6092

                #8
                Originally posted by Dormilich
                not as a real property, but you can use __get() to work around that.
                Not if he's using php 4, which I suspect he is. I know, in PHP 5, you can do $this->x = $x, even if you have not explicitly declared the $x member - not sure if this works in PHP 4, though.. I doubt it.

                Comment

                • Dormilich
                  Recognized Expert Expert
                  • Aug 2008
                  • 8694

                  #9
                  Originally posted by Markus
                  Not if he's using php 4, which I suspect he is.
                  then line #8 wouldn’t make sense… but yes, PHP4’s object support is rather, er, low.

                  Comment

                  • smartic
                    New Member
                    • May 2007
                    • 150

                    #10
                    my problem is i need parent class to hold dynamic property with out declaring it by var keyword,and also get the last property was added to the parent class here is my code :

                    Code:
                    class parts{
                    	// set part type & name
                    	public function set($type,$name){
                    		if(class_exists($type)){
                    			$this->{$name}['type'] = $type;
                    			return new $type();
                    		}
                    	}
                    }
                    class playstation extends parts{
                    	function speed(){
                    		// here is my problem & get last var
                    		print_r($this->emulator);
                    	}
                    }
                    $e = new parts();
                    // testing
                    $e->
                    	set('playstation','emulator')->
                    	speed('1028');

                    Comment

                    • Dormilich
                      Recognized Expert Expert
                      • Aug 2008
                      • 8694

                      #11
                      Originally posted by smartic
                      my problem is i need parent class to hold dynamic property with out declaring it by var keyword,and also get the last property was added to the parent class
                      working with unknown properties is a bit difficult, why don't you use an array for that?

                      Code:
                      class whatever
                      {
                          protected $props = array();
                      
                          public function __set($name, $value)
                          {
                              $this->props[$name] = $value;
                          }
                      
                          public function getLast($name)
                          {
                              return end($this->props);
                          }
                      }

                      Comment

                      • smartic
                        New Member
                        • May 2007
                        • 150

                        #12
                        because i want to access it from outside the class like that :
                        $e->emulator['type']; not $e->$props['emulator']['type'];

                        Comment

                        • Dormilich
                          Recognized Expert Expert
                          • Aug 2008
                          • 8694

                          #13
                          Originally posted by smartic
                          $e->$props['emulator']['type'];
                          that won't work, because the property is not public. let's extend the previous code a little bit.
                          Code:
                          class whatever
                          {
                              # the array where we store the properties
                              protected $props = array();
                          
                              # the definer function
                              public function __set($name, $value)
                              {
                                  $this->props[$name] = $value;
                              }
                          
                              # the accessor function
                              public function __get($name)
                              {
                                  if (isset($this->props[$name]))
                                  {
                                      return $this->props[$name];
                                  }
                                  throw new Exception(1, "The Property '$name' does not exist.");
                              }
                          
                              public function getLast($name)
                              {
                                  return end($this->props);
                              }
                          }
                          Code:
                          $e = new whatever;
                          $e->speed = 1028;
                          echo $e->speed;

                          Comment

                          Working...