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 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts

    the difference between $this->$var and $this->var

    Code:
    class sample
    {
        var $tempvar1;
        var $tempvar2;
        function sample()
        {
            $this->$tempvar1 = "hi";
            $this->tempvar2 = "hi";
        }
     
    }
    Hiya, simple stuff, what is the difference between the two above assignments?

    I'm guessing or feeling that $this->tempvar2 is a reference to the variable/properties of tempvar2, and $this->tempvar1 is a ref to the actual object it contains (if it were an object)

    Am i close?

  • #2
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i dont know that

    $this->$varname

    is even meant to be used.

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Posts
    165
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Does that mean it $this->$var should never be used?

    I can assign a variable value to it and print it out etc... created dblink to mysql then closed it, seems to work ok, if its never meant to be used, then why does it work? It must have some purpose?

    Just trying to clear up a few things about php so I know for certain what is, what can, and what isnt.

    Thx

  • #4
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    some things in php work, even though they shouldnt.

    eg:

    PHP Code:
    <?php

    echo hi;

    ?>
    will actually print hi, even though it is not a string.

    I used this example:

    PHP Code:

    class classname {

       var 
    $var;
        
       function 
    set(){
        
          
    $this->$var "Hello";
        
       }
        
       function 
    get(){
        
          return 
    $this->$var;
        
       }

    }

    $class = new classname();

    $class->set();

    echo 
    $class->get(); 
    prints Hello.

    however, i test on my local server, and have error reporting set to E_ALL, meaning it will display every tiny error it finds.

    both the:

    echo hi;

    example, and the class refrencing $this->$var give errors.

    My output from "echo hi;" was:


    Notice: Use of undefined constant hi - assumed 'hi' in C:\apache\www\TMPbbxlbophfo.php on line 3
    hi



    and from the class:


    Notice: Undefined variable: var in C:\apache\www\TMPbdde7ophhj.php on line 9

    Notice: Undefined variable: var in C:\apache\www\TMPbdde7ophhj.php on line 15
    Hello



    so referencing variables in a class should not be done $this->$var at all.

  • #5
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    ... unless its intentional eg as a dynamic method/function/property call , eg I do this quite a lot ... (class_exists() is a useful function to be using here)

    PHP Code:
    <?
    class whatever{
        function 
    method_farm$method $array_data ){
            
    $this->$method$array_data ) ;
        }
    }
    ?>
    so its valid if intentional
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  

    Posting Permissions

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