Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts

    Can a Class create a Variable ??

    I want to create a class which creates variables.
    (If someone has already done this, please provide a link or something.)

    The idea is to make a class that will instantiate variables and store a list of them so that they can all be unset in one fail swoop.

    This is how I envision the usage:
    PHP Code:
    $var =  new Variables();

    $var->new('testVar1','this is the content in this test variable');
    $var->new('testVar2',NULL);

    while(
    something blah blah){
        
    $testVar2.='some content';
    }

    if(
    $testVar2) echo $testVar1;

    $var->unset(all); 
    But I just can't figure out how to get it to return a Var like that.
    Let me know whether this needs more clarification.

    (I'm kinda hoping that this hasn't already been done, because it sounds like a fun exercise )

    ~ Mo

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    This could be done using the $GLOBALS variable.
    PHP Code:
    class Variables
    {

        protected 
    $variables = array();

        public function 
    __set$name$value )
        {
            
    $this->set$name$value );
        }

        public function 
    set$name$value )
        {
            
    $this->variables[$name] = $GLOBALS[$name] = $value;
            return 
    $this;
        }

        public function 
    clear()
        {
            
    $GLOBALS array_diff_key$GLOBALS$this->variables );
        }


    PHP Code:
    $var = new Variables;

    $var->set'name''John' );

    // or using __set():
    // $var->name = 'John';

    $name .= ' Smith';

    echo 
    $name// John Smith

    $var->clear();

    isset( 
    $name ); // FALSE 
    Last edited by kbluhm; 01-21-2010 at 06:24 PM.

  • #3
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    when using global variables (and I don’t mean the $GLOBALS array), always be aware of Code Injection. maybe you want to look at the (Abstract) Registry Pattern.

  • #4
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    So, getting back to this, what is the difference between using
    Quote Originally Posted by kbluhm View Post
    ...
    PHP Code:
    ...
        public function 
    __set$name$value )
        {
            
    $this->set$name$value );
        }
    ...
    ...
    $var->set'name''John' );
    ... 
    And this
    Quote Originally Posted by kbluhm View Post
    PHP Code:
    ...
        public function 
    set$name$value )
        {
            
    $this->variables[$name] = $GLOBALS[$name] = $value;
            return 
    $this;
        }
    ...
    ...
    // or using __set():
    // $var->name = 'John';
    ... 
    I'd like to learn why it works both ways so I can learn about those methodologies and become a more well rounded "developer" (I put that in quotes because that may be a bit of a stretch ).

    ~ Mo

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    __set() is a magic method for setting properties not already defined.

    http://php.net/manual/en/language.oop5.magic.php

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    the first one is a method call, the second a variable assignment.

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    Quote Originally Posted by kbluhm View Post
    __set() is a magic method for setting properties not already defined.
    it also works on non-public properties.

  • #8
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by Dormilich View Post
    it also works on non-public properties.
    Well ok, properties not publicly accessible.

  • #9
    Regular Coder mOrloff's Avatar
    Join Date
    Nov 2008
    Location
    The Great Pacific NW, USA
    Posts
    422
    Thanks
    8
    Thanked 6 Times in 6 Posts
    So much good information, so little time

    No, but honestly, thanks-a-bunch.
    If there's anything else, keep it coming.

    ~ mo

  • #10
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,342
    Thanks
    13
    Thanked 349 Times in 345 Posts
    you can use __set() & __get() to mimic read-only properties.


  •  

    Posting Permissions

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