PHP Universal Functions? Yes or No?
PHP Universal Functions? Yes or No?
I was wondering your opinions on having a PHP universal functions file. Does it literally make things easier and it is it a good thing to have? If you do not get what I mean by PHP universal functions file I mean a file with a ton of predefined functions like slimming down attack.php (the actual attacking mechanics like the rolls to a attack_creature();) that is what I mean.
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: PHP Universal Functions? Yes or No?
It is always a good idea to break down your code into the smallest pieces possible, but just throwing tons of them into a file would be bad organization most likely. Instead, put them into files dedicated to specific areas of your program (like attack functions, trade functions, item functions, etc.).
However, if you are going to go to those lengths, you may as well learn a bit of object-oriented programming, since OO is really the optimal way of organizing and compartmentalizing your PHP code.
However, if you are going to go to those lengths, you may as well learn a bit of object-oriented programming, since OO is really the optimal way of organizing and compartmentalizing your PHP code.
The indelible lord of tl;dr
Re: PHP Universal Functions? Yes or No?
I will look into a way to implement it into there. Thanks
Re: PHP Universal Functions? Yes or No?
my sites usually contain a single functions file. i find it easier that way personally because you dont have to go looking through files to find what you want lol.
Jack, do you know if its possible to access a previously set up instance of a class when making an ajax call to another script?
I.E
main.php
ajax.php
without the need to create the instance again.
Jack, do you know if its possible to access a previously set up instance of a class when making an ajax call to another script?
I.E
main.php
Code: Select all
$user = new loaduser();
Code: Select all
$user->adjusthp(10);
New Site Coming Soon! Stay tuned 

Re: PHP Universal Functions? Yes or No?
Well that`s not possible. PHP is stateless, so if you make an ajax call, you need to recreate a new object in jour AJAX script. The easiest way so far is creating a "state" in your database, bind a key to it, like $_SESSION['authentication_key']; and if you need to ajaxify something, add that key to your javascript.
So $key = $_SESSION['authentication_key'];
in your jS:
<script>
var Site = {};
Site : {
key : "<?php echo $key;?>",
};
</script>
In your ajax calls, send that key as well, in your PHP script, you can query the database, retrieve user data or specifics needed and do what you want.
So $key = $_SESSION['authentication_key'];
in your jS:
<script>
var Site = {};
Site : {
key : "<?php echo $key;?>",
};
</script>
In your ajax calls, send that key as well, in your PHP script, you can query the database, retrieve user data or specifics needed and do what you want.
Re: PHP Universal Functions? Yes or No?
Xaleph wrote:Well that`s not possible. PHP is stateless, so if you make an ajax call, you need to recreate a new object in jour AJAX script. The easiest way so far is creating a "state" in your database, bind a key to it, like $_SESSION['authentication_key']; and if you need to ajaxify something, add that key to your javascript.
So $key = $_SESSION['authentication_key'];
in your jS:
<script>
var Site = {};
Site : {
key : "<?php echo $key;?>",
};
</script>
In your ajax calls, send that key as well, in your PHP script, you can query the database, retrieve user data or specifics needed and do what you want.
Thanks Xaleph. I didnt think it was possible just wanted to check because I have never really played with OOP lol.
New Site Coming Soon! Stay tuned 

-
- Posts: 79
- Joined: Fri Dec 04, 2009 3:19 am
Re: PHP Universal Functions? Yes or No?
It is very important that you understand the reason you would not want to include functions to begin with, then you can go ahead and make a decision whether or not it's good for you.
I guess including single-function files are okay, they don't waste so much resource, but by using a single-file function list with all the functions you could ever want to use in your document - that means the client (webbrowser in this case) has to load the entire functions page, each time it is reloaded.
Imagine having a function file of 50-100 kb, whilst your actual page is only 2kb. Now these are very small amounts of data, but imagine 100 users continually using this single-source function list.
100 * 50kb = 4.88mb per 100[reload].
If this was the case, and your users are playing actively, it's pretty safe to assume your daily consumption would be something like..
500 megabytes per hour or just simply put: 11718.75 megabytes per 24 hour span.
And that is taking very low user amounts and refreshes in mind, I'm absolutely sure you will have more than 50kb per page if the game has a lot of images, chats, automatic updates etc.
So, think about it a few times and you'll come down to;
Use function files, but don't make a universal one, just make one that covers that specific page or action, such as maybe "mysql_fuctions.php", "database.php", "toplist_functions.php" etc!
I guess including single-function files are okay, they don't waste so much resource, but by using a single-file function list with all the functions you could ever want to use in your document - that means the client (webbrowser in this case) has to load the entire functions page, each time it is reloaded.
Imagine having a function file of 50-100 kb, whilst your actual page is only 2kb. Now these are very small amounts of data, but imagine 100 users continually using this single-source function list.
100 * 50kb = 4.88mb per 100[reload].
If this was the case, and your users are playing actively, it's pretty safe to assume your daily consumption would be something like..
500 megabytes per hour or just simply put: 11718.75 megabytes per 24 hour span.
And that is taking very low user amounts and refreshes in mind, I'm absolutely sure you will have more than 50kb per page if the game has a lot of images, chats, automatic updates etc.
So, think about it a few times and you'll come down to;
Use function files, but don't make a universal one, just make one that covers that specific page or action, such as maybe "mysql_fuctions.php", "database.php", "toplist_functions.php" etc!
Re: PHP Universal Functions? Yes or No?
Uhm, functions in PHP will not ever be included and flushed to the browser. Just a heads up. It`s all server side.
Re: PHP Universal Functions? Yes or No?
^ A good point
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
- Jackolantern
- Posts: 10891
- Joined: Wed Jul 01, 2009 11:00 pm
Re: PHP Universal Functions? Yes or No?
And even for client-side code, you should make one script for all pages because the browser cache will make it only have to be downloaded once.
The indelible lord of tl;dr