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;
}

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.