Page 1 of 1
Set a define through a function?
Posted: Mon Jan 31, 2011 10:39 am
by Domsore
Okay so this is what I currently have:
Code: Select all
<?php
/**
* Description of template
*
* @author Dominic Sore
*/
class template {
public function __construct() {
$this->defineTemp(Header);
}
public function defineTemp($funcId){
$funcDefSetUp = strtoupper($funcId);
$defSelf = 'TEMPL_';
$funcDef = $defSelf.$funcDefSetUp;
$funcCallSetUp = ucfirst($funcId);
$callSelf = 'self::templ';
$callBrak = '()';
$funcCall = $callSelf.$funcCallSetUp.$callBrak;
define($funcDef, $funcCall);
}
public function templHeader(){
echo 'Head Here';
}
}
?>
So effectively the defineTemp function should (assuming the $funcID is header) create:
Code: Select all
define(TEMPL_HEADER, self::templHeader());
This sort of does work.. when I just run TEMPL_HEADER; it does nothing so I tried echo TEMPL_HEADER; and it echos self::templHeader() as a string, but I want it to execute as a command... any ideas?
Cheers,
Dom
Re: Set a define through a function?
Posted: Mon Jan 31, 2011 10:47 am
by cpetercarter
I think however that you want the function to produce:
Code: Select all
define("TEMPL_HEADER", $this->templHeader());
Re: Set a define through a function?
Posted: Mon Jan 31, 2011 10:55 am
by Domsore
Still nothing, I var_dump(TEMPL_HEADER); and I get string(20) "$this->templHeader()"
Re: Set a define through a function?
Posted: Mon Jan 31, 2011 10:57 am
by John Cartwright
Change
Code: Select all
define(TEMPL_HEADER, self::templHeader());
to
Code: Select all
var_dump(self::templHeader());
exit();
// define(TEMPL_HEADER, self::templHeader());
What is the output?
Re: Set a define through a function?
Posted: Mon Jan 31, 2011 11:03 am
by Domsore
Hmn.. I get...
Fatal error: Cannot access self:: when no class scope is active in ------------------\index.php on line 13
I tried it with $this->templeHeader() but I got:
Fatal error: Using $this when not in object context in ----------------------\index.php on line 13
Re: Set a define through a function?
Posted: Mon Jan 31, 2011 11:08 am
by John Cartwright
You can only use self:: when you are in a static method -- and $this when inside the instance method of the object.
Post the entire code surrounding the problem code.
Re: Set a define through a function?
Posted: Mon Jan 31, 2011 11:27 am
by Domsore
Okay so I changed it so I have to type each one e.g:
Code: Select all
<?php
/**
* Description of template
*
* @author Dominic Sore
*/
class template {
public function __construct() {
define(TEMPL_HEAD , self::templHeader());
}
public function templHeader(){
echo 'Head Here';
}
}
?>
but now it automatically does self::templHeader();
Re: Set a define through a function?
Posted: Mon Jan 31, 2011 11:42 am
by John Cartwright
You are calling it statically from a class instance... Do you understand the difference?
Code: Select all
class template {
public function __construct() {
define('TEMPL_HEAD', $this->templHeader());
}
public function templHeader(){
echo 'Head Here';
}
}
Re: Set a define through a function?
Posted: Mon Jan 31, 2011 9:36 pm
by Domsore
I think I am very confused... so I'll show you every thing surrounding.
index.php ->
Code: Select all
<?php
session_start();
define(TPATH_BASE, dirname(__FILE__));
define(DS, DIRECTORY_SEPARATOR);
require_once (TPATH_BASE.DS.'library'.DS.'Global_Conf.php');
require_once (TPATH_BASE.DS.'library'.DS.'template.php');
$conf = new globalConf();
$tempProps = new template();
//* Template Defines *//
TEMP_HEADER;
?>
template.php ->
Code: Select all
<?php
/**
* Description of template
*
* @author Dominic Sore
*/
class template {
public function __construct() {
$this->setDefine();
}
public function setDefine(){
define(TEMPL_HEAD , $this->templHeader());
}
public function templHeader(){
echo 'Head Here';
}
}
?>
All I am trying to do is make calling a function easier for the someone who has no experience with php. So all they will have to do is (for example) type TEMPL_HEADER; to display the header.
Re: Set a define through a function?
Posted: Tue Feb 01, 2011 9:44 am
by John Cartwright
For the 3rd time, it's
Code: Select all
define('THIS_TEXT_SHOULD_BE_IN_QUOTES', ...);
Next, when your made your last changes, what is the value of TEMPL_HEAD after you define it?
Lastly, you do know you have to use an output function to display the contents.
Code: Select all
TEMPL_HEAD; //won't do anything
echo TEMPL_HEAD; //will output the contents
and just for the sake of debugging, try (post the output back to us)
I also noticed in your example you are using TEMP_HEADER, but your define is for TEMPL_HEAD
Re: Set a define through a function?
Posted: Tue Feb 01, 2011 10:00 am
by cpetercarter
You are almost there.
First, you need to be consistent about the name of the constant you are trying to set.
Second, the correct syntax for define() is:
ie "TEMPL_HEADER needs to be in quotes.
Third, the function templHeader() should
return the value "head here", not echo it.
In other words:
Code: Select all
<?php
/**
* Description of template
*
* @author Dominic Sore
*/
$tempProps = new template();
echo TEMPL_HEADER;
class template {
public function __construct() {
$this->setDefine();
}
public function setDefine(){
define('TEMPL_HEADER' , $this->templHeader());
}
public function templHeader(){
return 'Head Here';
}
}
?>
Re: Set a define through a function?
Posted: Wed Feb 02, 2011 4:42 am
by Domsore
Cheers guys, finally got it working... sorry if I frustrated you John. I appreciate the help from everyone.
Thanks,
Dom