PHP Universal Functions? Yes or No?

C++, C#, Java, PHP, ect...
Post Reply
User avatar
Nexus
Posts: 293
Joined: Sat Jun 18, 2011 10:42 pm

PHP Universal Functions? Yes or No?

Post by Nexus »

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.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP Universal Functions? Yes or No?

Post by Jackolantern »

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.
The indelible lord of tl;dr
User avatar
Nexus
Posts: 293
Joined: Sat Jun 18, 2011 10:42 pm

Re: PHP Universal Functions? Yes or No?

Post by Nexus »

I will look into a way to implement it into there. Thanks
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: PHP Universal Functions? Yes or No?

Post by Torniquet »

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

Code: Select all

$user = new loaduser();
ajax.php

Code: Select all

$user->adjusthp(10);
without the need to create the instance again.
New Site Coming Soon! Stay tuned :D
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: PHP Universal Functions? Yes or No?

Post by Xaleph »

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.
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: PHP Universal Functions? Yes or No?

Post by Torniquet »

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 :D
spawnfestis
Posts: 79
Joined: Fri Dec 04, 2009 3:19 am

Re: PHP Universal Functions? Yes or No?

Post by spawnfestis »

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!
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: PHP Universal Functions? Yes or No?

Post by Xaleph »

Uhm, functions in PHP will not ever be included and flushed to the browser. Just a heads up. It`s all server side.
User avatar
62896dude
Posts: 516
Joined: Thu Jan 20, 2011 2:39 am

Re: PHP Universal Functions? Yes or No?

Post by 62896dude »

^ A good point
Languages: C++, C#, Javascript + Angular, PHP
Programs: Webstorm 2017, Notepad++, Photoshop
Current Project: HP Destiny
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: PHP Universal Functions? Yes or No?

Post by Jackolantern »

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
Post Reply

Return to “Coding”