Set a define through a function?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Set a define through a function?

Post 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
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Set a define through a function?

Post by cpetercarter »

I think however that you want the function to produce:

Code: Select all

define("TEMPL_HEADER", $this->templHeader());
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Re: Set a define through a function?

Post by Domsore »

Still nothing, I var_dump(TEMPL_HEADER); and I get string(20) "$this->templHeader()"
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Set a define through a function?

Post 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?
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Re: Set a define through a function?

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Set a define through a function?

Post 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.
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Re: Set a define through a function?

Post 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();
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Set a define through a function?

Post 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';
   }
   
}
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Re: Set a define through a function?

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Set a define through a function?

Post 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)

Code: Select all

var_dump(TEMPL_HEAD);
I also noticed in your example you are using TEMP_HEADER, but your define is for TEMPL_HEAD
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: Set a define through a function?

Post 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:

Code: Select all

define ("NAME", value); 
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';
   }
   
}
?>
Domsore
Forum Commoner
Posts: 46
Joined: Wed Jan 26, 2011 7:07 pm

Re: Set a define through a function?

Post by Domsore »

Cheers guys, finally got it working... sorry if I frustrated you John. I appreciate the help from everyone.

Thanks,

Dom
Post Reply