1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Smarty/ OOP question

Discussion in 'PHP' started by Marshton, Aug 23, 2010.

  1. #1
    I have a class. Lets call it classA.

    
    class classA {
    public $smarty;
    
    __construct () {
    $this->smarty = new Smarty(); //Lets just assume that I've done all the necessary includes etc for smarty
    }
    }
    
    $classA = new classA;
    
    PHP:
    Basically what I'd like is Smarty to be acessible, using: $classA->smarty eg $classA->smarty->assign('blah','blah');

    I find that works up to a point, it will work if I do a $this->smarty->assign(); but it wont work if I do a $classA->smarty->assign();

    Am I messing up somewhere?

    Any ideas?
     
    Last edited: Aug 23, 2010
    Marshton, Aug 23, 2010 IP
  2. Kudo

    Kudo Peon

    Messages:
    933
    Likes Received:
    23
    Best Answers:
    0
    Trophy Points:
    0
    #2
    well, I suppose "assign" won't work because it's a function ... and functions need to be followed by () in order to work properly ..
     
    Kudo, Aug 23, 2010 IP
  3. Marshton

    Marshton Peon

    Messages:
    109
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    What do you mean? Please explain.
     
    Marshton, Aug 23, 2010 IP
  4. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #4
    You have syntax error, the constructor is a function:
    
    public function __construct( ) {}
    
    PHP:
    
    <?php
    class classA {
       public $smarty;
    
       public function __construct () {
          $this->smarty = new Smarty(); //Lets just assume that I've done all the necessary includes etc for smarty
       }
    }
    
    class Smarty {
       public function lol( ) { echo "LOL"; }
    }
    
    
    $classA = new classA;
    $classA->smarty->lol( );
    ?>
    
    PHP:
     
    Rainulf, Aug 23, 2010 IP
  5. Marshton

    Marshton Peon

    Messages:
    109
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    So, it should jsut be as simple as that?
     
    Marshton, Aug 23, 2010 IP
  6. Rainulf

    Rainulf Active Member

    Messages:
    373
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    85
    #6
    Yes, it is as simple as that. The reason yours didn't worked is because you forgot to add the "function" keyword before "__construct". Therefore, whenever you instantiate $classA, the constructor doesn't get called so object $smarty doesn't get instantiated.

    To put it short, you made a typo. ;)
     
    Rainulf, Aug 23, 2010 IP