samedi 13 février 2021

PHP7 const defined within a require (external file) within a conditional (if) is working - why?

While debugging a large body of code, I came across the following that was completely unrelated to my debugging. I did spend an inordinate amount of time Google searching for a plausible explanation:

The declaration of a const, within a function, within a conditional (if), within a require of an external file included. NOTE: this was NOT defined as part of a class construct!!

Since this const is defined within the context of a conditional, making it run-time? vs compile/parse-time, this should (IMHO) have given an error, but not only was there no error, but the code has been running perfectly in production for over three years!

Would love to hear expert explanations, or even a clue to what I am obviously missing!

Q1. Why is constant _DEFITEM not giving an error?

Q2. Why is constant _DEFITEM working even though it was not defined using define?

Thank you, in advance, and especially to JS, and for the wonderful work this community has done over the last decade!

REFERENCE:

PHP Manual

If an include() is conditional, will PHP include the file even if the condition is not met?

https://wordpress.stackexchange.com/questions/143439/conditionally-include-files-in-functions-php

CODE: Simple stub with a function that gets called, checks a passed-by-value parameter for "SAVE", conditionally including the (ALWAYS available) save module file, and then calling its main function. The included (require) save module file has the const declaration at the top.

Contrived, but accurate code representation below! Ellipses represent other code

//  main_stub3.php     php file processing client request (included within a main page/script)
<?php
function list_view_save_proc_stub3 (fn_param1, fn_param2...)  {
...
if ("VIEW" === fn_param2)  {
   ...
}
else
if ("SAVE" === fn_param2)  {
   ...
   require _INC . proj_save_module_file.php;           //  _INC is a global constant (path)
   sv_mod_fn ();
}
else
   ...
}  //  END fn list_view_save_proc_stub3
//  END main_stub3.php


//  proj_save_module_file.inc   include file that contains a const declaration and a function
const _DEFITEM = [ 'it_id' => 0, 'it_name' => '', ... ];   //  about ~50 items in array

function sv_mod_fn ()  {
//  some std error checking, building of a row, db connectivity and insertion +
//  message output to client
...
}

Aucun commentaire:

Enregistrer un commentaire