|
Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon. |
|
View previous topic :: View next topic |
Author |
Message |
janw Smarty n00b
Joined: 31 Aug 2016 Posts: 2
|
Posted: Wed Aug 31, 2016 2:58 pm Post subject: Variablen global definieren |
|
|
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 |
|
elpmis Smarty Elite
Joined: 07 Jun 2007 Posts: 321
|
Posted: Wed Aug 31, 2016 5:27 pm Post subject: |
|
|
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 |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Thu Sep 01, 2016 10:18 am Post subject: |
|
|
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 |
|
janw Smarty n00b
Joined: 31 Aug 2016 Posts: 2
|
Posted: Thu Sep 01, 2016 10:30 am Post subject: |
|
|
How can I pass data to a class in PHP?
Sincerly
Jan |
|
Back to top |
|
AnrDaemon Administrator
Joined: 03 Dec 2012 Posts: 1785
|
Posted: Fri Sep 02, 2016 12:16 am Post subject: |
|
|
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 |
|
|
|
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
|