Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

Variablen global definieren

 
Post new topic   Reply to topic    Smarty Forum Index -> Language: German
View previous topic :: View next topic  
Author Message
janw
Smarty n00b


Joined: 31 Aug 2016
Posts: 2

PostPosted: Wed Aug 31, 2016 2:58 pm    Post subject: Variablen global definieren Reply with quote

Hallo!

Ich möchte bei Aufruf von
Code:
 class Smarty_Project extends Smarty { ...
wie folgt Variablen zuweisen, auf die ich dann innerhalb des Templates immer und überall zugreifen kann.

Das mache ich so:
Code:
 
<?php
class Smarty_Project extends Smarty {

   function __construct()
   {
      parent::__construct();
                $userLanguage = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
               $this->assign('userLanguage', $userLanguage);
}
}
?>


Das funktioniert auch.

Nun möchte ich aber $userLanguage nicht erst in der construct-Function definieren, sondern schon vor der class-Erstellung. Damit ich auf die Variable $userLanguage auch aus anderen Bereichen zugreifen kann, die die Smarty_Project-Klasse nicht kennen.

Also quasi so
Code:
 
<?php
$userLanguage = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
class Smarty_Project extends Smarty {

   function __construct()
   {
      parent::__construct();
               $this->assign('userLanguage', $userLanguage);
}
}
?>


Nur funktioniert das nicht so, wie ich mir das wünschte. Innerhalb der Klassenkonstruktion ist $userLanguage unbekannt.

Mache ich etwas falsch oder geht das generell nicht?

Vielen Dank
Jan
Back to top
View user's profile Send private message
elpmis
Smarty Elite


Joined: 07 Jun 2007
Posts: 321

PostPosted: Wed Aug 31, 2016 5:27 pm    Post subject: Reply with quote

Code:
<?php
$userLanguage = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
class Smarty_Project extends Smarty {

   function __construct()
   {
      parent::__construct();
               $this->assign('userLanguage', $GLOBALS['userLanguage']);
}
}
?>


https://secure.php.net/manual/de/language.variables.scope.php
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1576

PostPosted: Thu Sep 01, 2016 10:18 am    Post subject: Reply with quote

Correct way of doing this is to pass the data required to your class explicitly.
Ele you create a hard dependency on user's outside knowledge and a potential for unwanted behavior in a long run.
What if user already have "$userLanguage" variable serving their own needs? Your class would just blow in their face.
Back to top
View user's profile Send private message
janw
Smarty n00b


Joined: 31 Aug 2016
Posts: 2

PostPosted: Thu Sep 01, 2016 10:30 am    Post subject: Reply with quote

How can I pass data to a class in PHP?

Sincerly
Jan
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1576

PostPosted: Fri Sep 02, 2016 12:16 am    Post subject: Reply with quote

1. Pass it to the constructor.
2. Use a dedicated setter method.
3. Use an implicit setter method. (Class::__set() magic method)
4. Just make the property public.

http://php.net/OOP
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> Language: German All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP