Use php files as functions?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Tchapman
Forum Newbie
Posts: 11
Joined: Wed Dec 01, 2010 3:38 am

Use php files as functions?

Post by Tchapman »

I will have a lot of function requirements, and instead of having one giant php file, was thinking about trying to have one main php file that has case statements and calls to other files that are acting like functions. There could be 50 or more functions.

I wanted to see if this is doable and something you guys consider good practice or bad.

This is oversimplified but the idea. A microprocessor sends out a string as needed to the dns to update the database like this:
string("main.php?OPCODE=1&PAR1=1, PAR2=2))

The main.php file would have a case statement:

Code: Select all

<html>
<body>

<?php
switch ($x)
{
case '$_REQUEST[OPCODE]' ":
  $file=fopen("1.php","r");
  break;
case '$_REQUEST[OPCODE]':
  $file=fopen("2.php","r");
  break;
case '$_REQUEST[OPCODE]':
  $file=fopen("3.php","r");
  break;
}
?>
</body>
</html> 
What I can't find in any tutorials I have seen is how I can forward any paramters or have the result returned by a call to a file except by echo ie:

Code: Select all

$file=fopen("3.php('$_REQUEST[PAR1]&'$_REQUEST[PAR2]')","r");
curlybracket
Forum Commoner
Posts: 59
Joined: Mon Nov 29, 2010 2:40 pm

Re: Use php files as functions?

Post by curlybracket »

I guess you need include() or require() instead of fopen. Also you don't have to pass $_REQUEST, it is visible in every included file.
Tchapman
Forum Newbie
Posts: 11
Joined: Wed Dec 01, 2010 3:38 am

Re: Use php files as functions?

Post by Tchapman »

Great, the include is perfect to save a lot of time.
Post Reply