English 中文(简体)
variable predefinition, moving variable to earlier point
原标题:

Ok, here s the deal where I m really stuck.

I use my cms with design patterns. Where s the last final output(in design template s main file: design.php ):

<div>{CONTENT_HEADER}</div>
<div style= float:left;width:25% >{CONTENT_LEFT}</div>
<div style= float:left;width:50% >{CONTENT_CENTER}</div>
<div style= float:left;width:25% >{CONTENT_RIGHT}</div>
<div>{CONTENT_FOOTER}</div>

Where is CONTENT_XXXX is generater thought site modules:

ob_start("gz_handler");
<....... LOAD ALL THE HEADER MODULES .......>
$output0 = ob_get_contents();
ob_end_clean();

ob_start("gz_handler");
<....... LOAD ALL THE LEFT SIDE MODULES .......>
$output1 = ob_get_contents();
ob_end_clean();

ob_start("gz_handler");
<....... LOAD ALL THE CENTER SIDE MODULES .......>
$output2 = ob_get_contents();
ob_end_clean();

ob_start("gz_handler");
<....... LOAD ALL THE RIGHT SIDE MODULES .......>
....
// That s I talk about, but this is just for static html, not variables
// Push to header finalizers to override default <title></title>
$header_finalizers_array[ change_head_title_attr_to ] = $thread_data[ topic_title ];
<....... END OF ALL THE RIGHT SIDE MODULES .......>
$output3 = ob_get_contents();
ob_end_clean();

ob_start("gz_handler");
<....... LOAD ALL THE FOOTER MODULES .......>
$output5 = ob_get_contents();
ob_end_clean();

define("CONTENT_HEADER", finalize_output($output0,$header_finalizers_array));
define("CONTENT_LEFT", finalize_output($output2,$left_finalizers_array));
define("CONTENT_CENTER", finalize_output($output3,$center_finalizers_array));
define("CONTENT_RIGHT", finalize_output($output4,$right_finalizers_array));
// No need finalizations, because there are no more parts, which call content changes
define("CONTENT_FOOTER", $output5);

All is ok in 95% of cases. But, there are some cases, where I need TO GET THE CONTENT OF VARIABLE(in left side modules) which ONLY will be defined in NEXT SIDE(ex. in right side modules)

With my code, I can define the site headers in header modules , but then if I want that, I m able to change it in ex. right side, because I m able add to array the replace data requirement, which will be executed after all the side content will be generater to variables but BEFORE printing in to client browser.

All this could be done only with 1 line in function:

function finalize_output($output="",$fin_array=array()) {
   <...>
   $output = preg_replace("#<title>.*</title>#i", "<title>".$fin_array[ change_head_title_attr_to ]."</title>", $output, 1);
   <...>
}

Now about my problem.

How to do the same with VARIABLES:

IN LEFT SIDE modules I have a sentence (PROBLEM: $object ):

if(isset($object)) {
  $best_product = sqlArray(sqlQuery("SELECT * FROM tableA WHERE b= $object "));
 <..All the rest code of BEST s product..>
}

IN RIGHT SIDE modules I have the sencence (PROBLEM: $object ):

<... Print LAST 10 PRODUCTS ***>
$res = sqlquery("SELECT * FROM tableA ORDER BY id DESC LIMIT 100);
while($data = sqlarray($res)) { <..PRINT PRODUCT INFO..> }

$new = rand(0,secured($_POST[ user_input_new_products ]));
for($i=1;$i<=$new;$i++) {
  $price_diff_old_new1 = change_products_to_sql( fish_$i , 19.99);
  $price_diff_old_new2 = push_product_to_sql( crab_$i , 16.99);
  if($i==$new) {
    $object = $price_diff_old_new1+$price_diff_old_new2;
  }
}

THIS IS JUST EXAMPLE CODE OF WHAT I NEED TO DO(so don t go to details), but THE POINT IS THAT, that I need somehow to submit the variable to earlier point of source: One of solutions would be " USE GOTO ", but is it variable will be remebered.

I mean:

echo "JOB IS STARTED";
  LABEL HOME:
   <INCLUDED FILE : procuts.php >
      if(isset($object) && check_is_number($object)) {
       $a = $object;
      }
      if(isset($break_me) && $break_me) {
        GOTO FINAL;
      }
   <END OF INCLUDE>
   ...

 <INCLUDED FILE : upload.php >
    $object = 999;
    $break_me = true;
    GOTO HOME;
  <END OF INCLUDE>
}
 LABEL FINAL: 
echo "JOB IS DONE";

AND, is there are any other way to do this WITHOUT GOTO(if GOTO solution is possible(?)) ?


I personally thought that is not possible to code it, but since Php 5.3.0 it is possible, I become likely to be coded. Misfortunelly I m not sure the company will use Php 5.3.0 on their servers, so I hope there is another solutions.

Also I m still not sure even the GOTO will help there.

问题回答

I suggest rethinking your architecture.

No offense, but the code looks rather messy to me.

To point out a few things:

  • you said you use Design Patterns, but the only pattern seems to be a Template View. You don t seem to be using MVC, nor a Two-Step View, which would both be appropriate in your case.
  • is there any particular reason why you are not using OOP? It s not that procedural coding is a bad thing, but proper encapsulation of things that conceptually go together and separation of concerns will make your app much more maintainable.
  • your variable names are not meaningful. An $output0 really tells you nothing about what is inside. Why does $object hold a number? The same is true for your function names: push_to_sql() and change_to_sql() sound alike. I guess one means insert and one means update, but the semantics could be much clearer.
  • Why are you assigning the variables holding your content-blocks to constants? That step is completely superfluous as you can use the variables in your template view as well.

Now, I understand you used example code in your question s description, so bear with me if I addressed points not applicable to your production code. But still, the simple fact that your modules cannot be rearranged without affecting the other indicates a code smell. Modules should be self-contained.

But let s get to your issue now.

You say you need to submit the variable to an earlier point in the source. I do not understand the example code enough to figure out why. But if you have to keep your Big Ball of Mudcurrent architecture, you could refactor all the code that will be shared among modules into a separate function. Then preload all required data and inject it into the functions rendering the content-blocks. This way, you should be able to push them around as wanted.

If you don t have to keep your current architecture, do yourself a favor and have a look at the patterns linked above. Most PHP frameworks utilize them and you might want to consider migrating your app onto one. To further improve your code, have a look at these QA tools and check out these sites:





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...