Jump to content

[SOLVED] Not a problem, just an explanation needed really.


berridgeab

Recommended Posts

Hi

 

I am writing quite a large web application which is going to grow larger and larger as it's purpose continues to expand.I was going through some old scripts, and optimizing them to use as least code as possible.

 

One large script I have is called admin.php. Though the initial script runs through a small bit of code to determine which function to use. The acutal functions themselves are growing quite large and i'm worryed about the time PHP will take to parse the entire script. I have written an example down below of what it looks like in its basic form.

 

<?php

$Page = $_REQUEST['page'];
switch ($Page)
{
case 'Page1':
    function 1();
    break;
case 'Page2':
    function 2();
    break;
case 'Page3':
    function 3();
    break;
}

function 1()
{
//Lots of code and doing stuff
}

function 2()
{
//Lots of code and doing stuff
}

function 3()
{
//Lots of code and doing stuff
}

 

Basically my question is, say $Page matched Page1. Would only function 1() be parsed as the other functions are not required, or would PHP parse the entire including all it's functions (Even though there not required at the time).

 

If the answer is Yes, then is there any way round this? Maybe spliting chunks of my script into different PHP files and just including them? (Like Below). Will this stop the unneeded bits of code being parsed?

 

<?php

$Page = $_REQUEST['page'];
switch ($Page)
{
case 'Page1':
    include_once('admin_func1.php');
    break;
case 'Page2':
    include_once('admin_func2.php');
    break;
case 'Page3':
    include_once('admin_func3.php');
    break;
}

 

 

Parsing to bytecode is pretty quick, so not something you normally need to worry about. If it makes you feel better, you could probably split your functions into include files, but the overhead of searching for and reading the file would probably be greater than having them all in one file

I'm not sure about the parsing but my guess is that it will parse all of the code.

 

Seems to me like you have a better method already. If you do the select case to include() the script for each page as it's required not only will you compartmentalize your scripts making them easier to manage and update but the parsing issue becomes moot.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.