/    Sign up×
Community /Pin to ProfileBookmark

Access PHP From Inside Ajax

Currently I have all of my main php functions stored in a single file called allfunctions.php. This file is included within the page header.

My problem is when I use ajax to load in a segment of my page elements within this segment require some of the functions from my allfunctions.php file which they obviously can’t access (as far as I know) due to being separately loaded ajax content.

The only solution seems to be to include my file within every bit of ajax I intend to load in. Do I have any other options here or is that how it is generally done or should I just include the functions required within that ajax file?

My solution currently is to declare a php variable right at the top of my index page called $JS and then within each ajax content file I load in I can include the following:-

[CODE]

if(!$JS){
include(“allfunctions.php”);
}

[/CODE]

Am I going along the right lines?

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 19.2012 — While it is hard to know the "best" answer with minimal information, in general I would suggest only putting related functions into an include file, and therefore possibly have multiple include files. Then you would only include whichever function groups you need as and where you need them. This is similar to the common practice in object-oriented PHP of having each class definition in its own file. In fact, my ultimate solution might be to group those related classes into classes, in which case you could then set up an __autoload() function to handle loading of them as/where needed.
Copy linkTweet thisAlerts:
@jimmyoneshotauthorJan 21.2012 — Sounds good. So basically I can perhaps have a folder called php functions which contains multiple include files based around function groups i.e. each include file contains functions that are always used together. Then I could just include these files as and where required.

I'll look into the autoload function aswell. Thanks. ?
Copy linkTweet thisAlerts:
@NogDogJan 21.2012 — Yep. Then you're already on the first step (of several) toward object-oriented PHP. ?

The next step would be to define those related functions as static methods within a class:

<web root>/functions/filestuff.php:
[code=php]
<?php
/**
* Example of a not quite OOP approach
*/

/**
* Collection of static methods for working with files
*/
class FileStuff
{
/**
* Read a file and return its contents
* @return string
* @param string $path
*/
public static function read($path)
{
return file_get_contents($path);
}

/**
* Write string to a file
* @return bool
* @param string $path
* @param string $data
*/
public static function write($path, $data)
{
return file_put_contents($path, $data);
}

/**
* Delete a file
* @return bool
* @param string $path
*/
public static function delete($path)
{
return unlink($path);
}
}
[/code]

Then you can set up an __autoload() in your main script and simply call the class::methods as needed:

[code=php]
<?php
function __autoload($class)
{
require $_SERVER['DOCUMENT_ROOT'].'/functions/'.strtolower($class).'.php';
}

$someFile = 'path/to/file.txt';
$text = FileStuff::read($someFile);
FileStuff::delete($someFile);
[/code]


This is not truly object-oriented since we never actually instantiate a FileStuff object, but it is at least a fair way along that path and makes use of some of the features of PHP classes/objects, and it could easily be refactored at some point to be a full-blown OOP approach to working with files in PHP.
×

Success!

Help @jimmyoneshot spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...