SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Digging through Symfony 1.4

    Hi,

    I'm wondering if anyone knows how Symfony does the following:

    PHP Code:
    class someActionClass extends sfActions {
      public function 
    executeLists() {
        
    $this->fname 'John';
        
    $this->lname 'Doe';
      }

    And then when the view is called, all I need to do is echo $fname / $lname to get the value of those variables. What confuses me is that they are using $this as of it was a property and then some how it changes to just a variable.

    If anyone can clear that up please

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    I don't know how SF does it, but one way to it would be to not use instantiated class properties (i.e. no public $lname; in the class) but set and get the values of virtual properties in an array and then export extract that array when you render the view file.

    Sounds more confusing than it is:

    PHP Code:
    class MyClass
    {
       private 
    $properties;
       
       public function 
    __set($key$value)
       {
           
    $this->properties[$key]=$value;
       }

       public function 
    __get($key)
       {
           return 
    $this->properties[$key];
       }

       public function 
    render($viewFile)
       {
          
    extract($this->properties);
          include(
    $viewFile);
       }

    Something like that
    Last edited by ScallioXTX; Dec 1, 2010 at 18:17. Reason: not export ... extract
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Zealot
    Join Date
    Apr 2010
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah yes, that would work! BTW, did you mean extract() ?

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    I did
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    class TestThing {
      public 
    $fname 'John',
             
    $lname 'Doe';
    }

    $c = new TestThing;
    extract( (array)$c ); # Typecasting to array probably not required.

    var_dump$fname$lname );

    # This might also work:

    class TestThing {
      public 
    $fname 'John',
             
    $lname 'Doe';

      function 
    load ()
      {
        
    extract( (array)$this );
        include 
    '...';
      }

    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Yes, that would also work. But I don't really like it because it also extracts private variables, and I like my private variables private
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  7. #7
    ¬.¬ 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 ScallioXTX View Post
    Yes, that would also work. But I don't really like it because it also extracts private variables, and I like my private variables private
    Only if you use "$this" if done outside of the class it will only have public parameters.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



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
  •