nV News Forums

 
 

nV News Forums (http://www.nvnews.net/vbulletin/index.php)
-   Software Development (http://www.nvnews.net/vbulletin/forumdisplay.php?f=53)
-   -   PHP Question (http://www.nvnews.net/vbulletin/showthread.php?t=91451)

ViN86 05-14-07 07:53 AM

PHP Question
 
im getting into php and plan to use it for managing a MySQL database and some other things. (adding permissions, login control, email mailing list, activity log, etc.)

im looking to make a standard function for adding entries to the database. i shouldnt have a problem creating the function.

however, is it possible for me create the function in a location and then access it from other .php files, or do i have to write the function into each php file? the latter sounds like a waste of space as well as extra time spent processing the page.

anyone know how to reference functions in another .php file?

evilghost 05-14-07 08:36 AM

Re: PHP Question
 
Sure, simply create a separate page called "functions.php" and on each PHP page where you require the use of the function issue the following:

require("functions.php");

It's really that easy. :)

Remember, any time you pass a variable to MySQL make sure you mysql_escape_string() first to avoid the possibility of SQL injection attacks.

superklye 05-14-07 09:17 AM

Re: PHP Question
 
I <3 includes :D

ViN you're putting your security/login session checks as an include file as well, right?

ViN86 05-14-07 01:48 PM

Re: PHP Question
 
Quote:

Originally Posted by evilghost
Sure, simply create a separate page called "functions.php" and on each PHP page where you require the use of the function issue the following:

require("functions.php");

It's really that easy. :)

Remember, any time you pass a variable to MySQL make sure you mysql_escape_string() first to avoid the possibility of SQL injection attacks.

thx :D that's awesome.

Quote:

Originally Posted by superklye
I <3 includes :D

ViN you're putting your security/login session checks as an include file as well, right?

well, i havent really learned all that yet and havent written the database nor the SELECT/INSERT/UPDATE/DELETE statements/functions i need yet.

ill be back here when i need help with preventing SQL injection attacks. thx guys :)

evilghost 05-14-07 01:49 PM

Re: PHP Question
 
Post you're code, we'd be happy to review it with you especially for security concerns/etc.

fivefeet8 05-16-07 02:05 AM

Re: PHP Question
 
Quote:

Originally Posted by evilghost
Sure, simply create a separate page called "functions.php" and on each PHP page where you require the use of the function issue the following:

require("functions.php");

It's really that easy. :)

Isn't it better to use:

require_once('functions.php'); ?

supra 05-16-07 03:57 AM

Re: PHP Question
 
Quote:

Originally Posted by fivefeet8
Isn't it better to use:

require_once('functions.php'); ?

dont think it matters too much unless that file is referenced more then once in the script

evilghost 05-16-07 10:44 AM

Re: PHP Question
 
Quote:

Originally Posted by supra
dont think it matters too much unless that file is referenced more then once in the script

Exactly, IMHO it would be poor coding to include the function more than once. I could see a use for require_once() in a conditional but in this case I would assume require() would be more fitting with the file inclusion occurring at the "top" of the code outside of any conditional.

atriq 05-16-07 12:55 PM

Re: PHP Question
 
In practice, I usually use require/include_once() just in the event the chain of them end up referencing them multiple times. Solves a lot of "function redefined" statements.

fivefeet8 05-16-07 01:37 PM

Re: PHP Question
 
Quote:

Originally Posted by evilghost
Exactly, IMHO it would be poor coding to include the function more than once. I could see a use for require_once() in a conditional but in this case I would assume require() would be more fitting with the file inclusion occurring at the "top" of the code outside of any conditional.

I've found that once you start programming in OOP with PHP. It can get messy when multiple classes requires the same functions/classes from other php files, but you needed to use both classes on one page. For example:

All objects(classes) are defined on different php scripts:
Object 1 requires object2;
Object 3 requires object2;

Your page requires object1, object2, and object3. Using only require('xx.php') would cause errors. I guess it would depend on what you're trying to do though.

evilghost 05-16-07 02:06 PM

Re: PHP Question
 
Quote:

Originally Posted by fivefeet8
I've found that once you start programming in OOP with PHP. It can get messy when multiple classes requires the same functions/classes from other php files, but you needed to use both classes on one page. For example:

All objects(classes) are defined on different php scripts:
Object 1 requires object2;
Object 3 requires object2;

Your page requires object1, object2, and object3. Using only require('xx.php') would cause errors. I guess it would depend on what you're trying to do though.

Good point.

tieros 05-17-07 11:21 AM

Re: PHP Question
 
If you're using PHP5, I'd suggest putting that core code into a class, and then putting the class file somewhere in your autoinclude path. That way, you don't need to require() or include() it, or worry about instantiation issues from other files.

A call like:
$sess = new SessionHandler();

will look in your autoinclude path for a file named SessionHandler.php, read it in if it's not already instantiated, and call it.

Putting the code in classes also keeps variable scoping a little cleaner plus a few other nice freebies. Using classes had a bit too much overhead in PHP4, but they are great in 5.


All times are GMT -5. The time now is 06:35 AM.

Powered by vBulletin® Version 3.7.1
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright 1998 - 2014, nV News.