SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    dynamically declare methods

    Is there any way (in PHP4) to dynamically declare class methods?

  2. #2
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gavwvin
    Is there any way (in PHP4) to dynamically declare class methods?
    Why would you ever want to do that?

    Or, rather, if you need something like that, your application is poorly designed.

  3. #3
    SitePoint Addict mx2k's Avatar
    Join Date
    Jan 2005
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    class test 
    {
        function 
    write $arrArgs null )
        {
            print 
    'hello word';
        }
    }

    function 
    callMethod $strCallTestMethod$arrArgs null ) {

        (object) 
    $x;
        
    $x = new test;
        
    $x -> $strCallTestMethod$arrArgs );
    }

    callMethod 'write' ); 
    be careful/mindful in how you structure your signatures.

    and if you wanted to you could do the same with the object, just make a place for it in the function's signature

    function callMethod ( $strClassName, $strCallMethod, $arrArgs = null )

  4. #4
    SitePoint Guru gavwvin's Avatar
    Join Date
    Nov 2004
    Location
    Cornwall, UK
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your replies- what I'm trying to do is write an abstract class which provides the basic functionality dealing with a row in a database- get_from_id, get, set properties, update, insert, delete etc. What I want to be able to do is in the constructor of the child class define the table, primary key and also define all the properties of this object:
    PHP Code:
    class user extends record {
      function 
    user() {
        
    $this->set_table("users");
        
    $this->set_primary_key("user_id");
        
    $this->add_field("user_name","name"); // this assigns the column user_name to the property $name
        
    $this->add_field("user->email","email");
        
    //.... etc
      
    }

    Now I would like to be able to call the value of the user's email like this $user->email() or their name like this $user->name() etc- which allows me more control over the way it is out put than simply echoing the property itself. I could of course define a function for each property in the child class but this is tedious (especially if there are about 50 fields in the db) and not ideal as I have to code the name of the property more than once- so I want the "record" parent class to define a basic version of these functions for each field/property in the add_field() method- something like this.
    PHP Code:
    function name() {
       return 
    $this->get('name');
    }
    //where get() simply returns the property 
    If the way I wanted a property returned was different (say for instance applying a smilies or bbcode function) I could just define the function in the child class as a special case and overwrite the basic function in the parent class and I wouldn't have to change the implementation at all- its still called by $user->name() or $user->email() or whatever.
    I know this can be done with overloading in PHP5 but I wasn't sure what I could do in PHP4.
    Last edited by gavwvin; May 5, 2005 at 06:37.

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I want to be able to do is in the constructor of the child class define the table, primary key and also define all the properties of this object
    Have you taken a look at the Decorator pattern, as this looks like an ideal situation where it could help you

    You could swap in different decorators, where you need to access data differentials.


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
  •