I'm developing an app where I have cleanly
separated HTML and PHP, as well as SQL
and PHP...
I include files where I set a "resource" associative array. My PHP classes refer to the array for things such as SQL queries. One of my favorite scripting practices is to use here-docs. The problem - any way to hold off on variables expanding in a here-doc until they're actually defined?
(this is within the same included file..)
// this is fine... $VALS['collection_id'] is defined..
$R["SQL_GET_COLLECTION"] =<<<EOD
select collection_dir from collection
where collection_id = {$VALS['collection_id']}
EOD;
// this is not.. $VALS['save_h'] has not yet
// been calculated...
$R["SQL_SAVE_SIZES"] =<<<EOD
update photo
set original_height = "{$VALS['save_h']}",
...etc...
EOD;
So, you see, there's a dependency...any var put into the contents of a here-doc gets expanded right away. Or... is there some other approach, perhaps involving variable-variables?
There are several things I can do:
I can split up statements into separate include files, not dragging in addtional $R elements (the class refers to them as $RSRC) until the variables referred to within a given here-doc are assigned. (what happens in this case is that we call into MySQL once to get a collection_id mapping to a filesystem directory.. later we call MySQL to update a record for a photo)
I could do something like:
$R["SQL_SAVE_SIZES"] =<<<EOD
update photo
set original_height = "FP_VAL_save_h",
... and then search/replace "FP_VAL_save_h" with the correct val, once it is defined.
- I could \$escape my variables during here-doc assignment, and later eval them into expanding within the string
Question: What are some other approaches? Something that feels like a here-doc, but which expands upon reference, as opposed to assignment? (hope that is clear)
thanks for any ideas!