FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Usage of class constants inside strings and heredocs
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Usage of class constants inside strings and heredocs [message #170490 is a reply to message #170489] Tue, 02 November 2010 16:47 Go to previous messageGo to previous message
alvaro.NOSPAMTHANX is currently offline  alvaro.NOSPAMTHANX
Messages: 277
Registered: September 2010
Karma:
Senior Member
El 02/11/2010 17:14, Giuseppe Sacco escribió/wrote:
> I created a class that encapsulates all methods for accessing a
> database table. That class have class constants for defining all table
> fields. I would like to create strings that include those constants,
> but I fail to see how I might do it.
>
> a simple (and not working) example:
>
> class myTable {
> const F_CODE = "code";
> const F_DESCRIPTIOM = "description";
> const TABLENAME = "lookup";
> public function insert($record)
> {
> $sql =<<<SQLQ
> INSERT INTO {self::TABLENAME}
> VALUES ({$record[self::F_CODE]},{$record[self::F_DESCRIPTION]}
> SQLQ;
>
> // ....
> } // end method insert
> } // end class myTable

"Not working" is a too simplistic description. Let's fix some syntax
errors and run your cod:

<?php

class myTable {
const F_CODE = "code";
const F_DESCRIPTION = "description";
const TABLENAME = "lookup";
public function insert($record)
{
$sql = <<<SQLQ
INSERT INTO {self::TABLENAME}
VALUES ({$record[self::F_CODE]},{$record[self::F_DESCRIPTION]})
SQLQ;

echo $sql;
} // end method insert
} // end class myTable

$record = array(
'code' => 'This is the code',
'description' => 'This is the description',
);
$foo = new myTable;
$foo->insert($record);

?>

INSERT INTO {self::TABLENAME}
VALUES (This is the code,This is the description)



> So, again, my question is: how do I put constants inside strings or
> heredocs?

Here's the documentation for the heredoc syntax:

http://es.php.net/manual/en/language.types.string.php#language.types.string .syntax.heredoc

Scroll down to "Variable parsing". As the title suggests, PHP only
parses variables, but there's a note to be taken into account:

"Functions, method calls, static class variables, and class constants
inside {$} work since PHP 5. However, the value accessed will be
interpreted as the name of a variable in the scope in which the string
is defined. Using single curly braces ({}) will not work for accessing
the return values of functions or methods or the values of class
constants or static class variables."

In simple words: it parses {self::TABLENAME} and then looks for a local
variable called $lookup. There is no replacement for two reasons:

- The syntax should be {${self::TABLENAME}}
- There is no such local variable

This does work as expected:

$tablename = self::TABLENAME;
$f_code = $record[self::F_CODE];
$f_description = $record[self::F_DESCRIPTION];

$sql = <<<SQLQ
INSERT INTO $tablename
VALUES ($f_code,$f_description)
SQLQ;

Make sure that heredoc is simplifying your code and not the opposite. To
build a simple templating system there're other alternatives like
sprintf() or strtr().


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Read Message
Previous Topic: Free Web Space for our Experiments
Next Topic: There is some framework/tool to generate PHP code
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Thu Nov 14 03:23:54 GMT 2024

Total time taken to generate the page: 0.05168 seconds