SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    string -> pass arguments to object

    Hi,

    I have a string as followed:

    PHP Code:
    $a '4,8'
    "4" is an argument, "8" is another argument, both to be passed to an object whose constructor expects two arguments.

    PHP Code:
    $obj = new Obj($a); 
    How can I achieve this at instantiation level? The object thinks there is one argument "4,8".

    There is one no-no: I don't want to touch the class using the two arguments.


    Regards,

    -jj.

  2. #2
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why would you need this?

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,508
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    list($a1, $a2) = explode(',', $a);
    $obj = new Obj($a1, $a2);

  4. #4
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hmm perplexing, an exam question?

    What about this?

    PHP Code:
    <?php
    eval("\$obj = new Obj({$a});")
    ?>
    SilverB.

  5. #5
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You could use:
    PHP Code:
    <?php
    class Obj{
        protected 
    $a$b;
        function 
    __Construct($a$b null){
            if(
    $b == null){
                
    $arr explode(','$a);
                
    $a trim($arr[0]);
                
    $b trim($arr[1]);
            }
            
    $this->$a;
            
    $this->$b;
        }
    }
    PHP Code:
    //all have the same effect:
    $obj = new Obj('4,8');
    $obj = new Obj(' 4 , 8 ');
    $obj = new Obj(48); 
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  6. #6
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverB
    eval
    A shadow has been cast over this thread!
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    *giggles mischievously*

    Would it work though? I haven't tested...does that count as two shadow i wonder!

    SilverB.

  8. #8
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Yep, it would work.

    However I really wouldn't recommend using it, for speed and security reasons.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Yay! Sussed it.

    PHP Code:
    <?php

    class breakfast
    {
        public 
    $sText;
        
        public function 
    __construct$firstItem $secondItem )
        {
            
    $this->sText "I'll order {$firstItem} omelette with {$secondItem} please!";
        }
    }

    $sParams 'cheese,bacon';

    eval(
    "\$oBreakfast = new breakfast({$sParams});");

    echo 
    $oBreakfast->sText;

    ?>
    However I really wouldn't recommend using it, for speed and security reasons.
    I completely agree, but I do love challenges!

    SilverB.

  10. #10
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    eval nah no need for that.
    PHP Code:
    $args 'a,b'$args explode','$args );

    $o = new ReflectionClass'Obj' );
    $r $o->newInstanceArgs$args );

    var_dump$r ); 
    I love playing with the Reflection API <3
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  11. #11
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    65 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $a '4,8';

    $ref = new ReflectionClass('Obj');
    $obj $ref->newInstanceArgs(explode(','$a)); 
    edit: logic_earth beat me to it

  12. #12
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    I love playing with the Reflection API <3
    Geek!

  13. #13
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Geek!
    I'll accept that label with pride!
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  14. #14
    SitePoint Wizard
    Join Date
    Jan 2005
    Location
    blahblahblah
    Posts
    1,447
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    eval nah no need for that.
    PHP Code:
    $args 'a,b'$args explode','$args );

    $o = new ReflectionClass'Obj' );
    $r $o->newInstanceArgs$args );

    var_dump$r ); 
    I love playing with the Reflection API <3

    I knew you would come up with something amazing like this!


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
  •