How to make PHP Code more Object Oriented

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
vitinho444
Posts: 2825
Joined: Mon Mar 21, 2011 4:54 pm

How to make PHP Code more Object Oriented

Post by vitinho444 »

Hello guys, near of the official release for my Browser Game (the first officially) i got myself into some errors due to code that it's not the best to read :)

Well.. I fixed those errors with your(community) help and now i have another question about OOP in PHP.

My main page for the village (its a village game) where you have all the buildings info and stuff, has more than 930 lines of code and the majority of those lines are like this :

To show the Palace Info

Code: Select all

echo "<tr><td><form method='post' action='palacio.php'><input type='hidden' name='villageid' value='$id'><input type='image' src='images/buildings/" . $age . "/" . $palacio_info['image'] . "' alt='Submit button'></form></td><td>" . $lang['Buildings_palacio_desc'][$language]
                	. "</td><td>" 
                	. $lang['Buildings_cost_stone'][$language] . $palacio_cost_stone . "<br>"
                	. $lang['Buildings_cost_wood'][$language] . $palacio_cost_wood . "<br>"
                	. $lang['Buildings_cost_argila'][$language] . $palacio_cost_argila . "</td><td>"
                	. $lang['Aldeias_nivel'][$language] . $palacio . "<br>"
                	. $lang['Tempo'][$language] . round($palacio_tempo / 60,2) . "min
                	<form method ='post' action='aldeia.php'><input type='hidden' name='village_name' value='$nome'>
                	<input type='hidden' name='building' value='palacio'>
                	<input type='hidden' name='time' value='$palacio_tempo'>
                	<input type='hidden' name='aldeia' value='$id'>
                	<input type='hidden' name='cost_stone' value='$palacio_cost_stone'>
                	<input type='hidden' name='cost_wood' value='$palacio_cost_wood'>
                	<input type='hidden' name='cost_argila' value='$palacio_cost_argila'>
                	<input type='submit' style='height:2em; width:5em;' value='" . $lang['Buildings_Levelup'][$language] . "'>
                	</form>
                	</td><td></tr>";
to show an engineer (need to check if its built):

Code: Select all

if($engenheiro)
                	{
                	echo "<tr><td><form method='post' action='engenheiro.php'><input type='hidden' name='village_id' value='$vil_id'><input type='image' src='images/buildings/" . $age . "/" . $engenheiro_info['image'] . "' alt='Submit button' name='bla' value='$vil_id'></form></td><td>" . $lang['Buildings_engenheiro_desc'][$language]
                	. "</td><td>" 
                	. $lang['Buildings_cost_stone'][$language] . $engenheiro_cost_stone . "<br>"
                	. $lang['Buildings_cost_wood'][$language] . $engenheiro_cost_wood . "<br>"
                	. $lang['Buildings_cost_argila'][$language] . $engenheiro_cost_argila . "</td><td>"
                	. $lang['Aldeias_nivel'][$language] . $engenheiro . "<br>"
                	. $lang['Tempo'][$language] . round($engenheiro_tempo / 60,2) . "min
                		<form method ='post' action='aldeia.php'><input type='hidden' name='village_name' value='$nome'>
                	<input type='hidden' name='building' value='engenheiro'>
                	<input type='hidden' name='time' value='$engenheiro_tempo'>
                	<input type='hidden' name='aldeia' value='$id'>
                	<input type='hidden' name='cost_stone' value='$engenheiro_cost_stone'>
                	<input type='hidden' name='cost_wood' value='$engenheiro_cost_wood'>
                	<input type='hidden' name='cost_argila' value='$engenheiro_cost_argila'>
                	<input type='submit' style='height:2em; width:5em;'value='" . $lang['Buildings_Levelup'][$language] . "'>
                	</form>
                	</td><td></tr>";
                	}
                	else
                	{
                	echo "<tr><td><img src='images/buildings/" . $age . "/" . $engenheiro_info['image'] . "'></td><td>" . $lang['Buildings_engenheiro_desc'][$language]
                	. "</td><td>" 
                	. $lang['Buildings_cost_stone'][$language] . $engenheiro_cost_stone . "<br>"
                	. $lang['Buildings_cost_wood'][$language] . $engenheiro_cost_wood . "<br>"
                	. $lang['Buildings_cost_argila'][$language] . $engenheiro_cost_argila . "</td><td>"
                	. $lang['Aldeias_nivel'][$language] . $engenheiro . "<br>"
                	. $lang['Tempo'][$language] . round($engenheiro_tempo / 60,2) . "min
                		<form method ='post' action='aldeia.php'><input type='hidden' name='village_name' value='$nome'>
                	<input type='hidden' name='building' value='engenheiro'>
                	<input type='hidden' name='time' value='$engenheiro_tempo'>
                	<input type='hidden' name='aldeia' value='$id'>
                	<input type='hidden' name='cost_stone' value='$engenheiro_cost_stone'>
                	<input type='hidden' name='cost_wood' value='$engenheiro_cost_wood'>
                	<input type='hidden' name='cost_argila' value='$engenheiro_cost_argila'>
                	<input type='submit' style='height:2em; width:5em;'value='" . $lang['Buildings_Construct'][$language] . "'>
                	</form>
                	</td><td></tr>";
                	}
This is just a piece of the code.. imagine an error in there.. my head would KABOOOOOOOOOOOM xD

Thank god i did it good at first.. at least i think so.

Well im here to ask you guys how can i make this more Object Oriented..

If you know C++ you will know what im talking about like to present this kind of information:

Code: Select all

void ShowInformation()
{
PalaceInfo();
EngineerInfo();

...etc
}

I'm not saying i will use this now.. because... hey.. 930 lines of code.. re doing those? no thank you.. but in the future it would be cool to use.
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
overras
Posts: 91
Joined: Sun Oct 03, 2010 7:25 am

Re: How to make PHP Code more Object Oriented

Post by overras »

"more oop" is not "more efficient" , you must know that. But, yes, you can do better that code.

First time you can do this, replace variables with objects. Ex: $palacio varbiale < (after edit) > $this->palacio. If you want more , you can create functions to write every input and button etc etc but I don't recommend you this. Remember!
User avatar
vitinho444
Posts: 2825
Joined: Mon Mar 21, 2011 4:54 pm

Re: How to make PHP Code more Object Oriented

Post by vitinho444 »

I see in C++ this all the time.. but what does it refers to?

in this case $this->palacio means this (query for village info) then palacio?
My Company Website: http://www.oryzhon.com

Skype: vpegas1234
Mardonis
Posts: 139
Joined: Wed Jun 29, 2011 7:54 pm

Re: How to make PHP Code more Object Oriented

Post by Mardonis »

I happened to find this for you incase it may help you. http://www.killerphp.com/tutorials/object-oriented-php/
jeffreybiles
Posts: 17
Joined: Sun Nov 18, 2012 9:58 pm

Re: How to make PHP Code more Object Oriented

Post by jeffreybiles »

Have you considered Ruby on Rails or Node.js? A bit of a switch, and not *exactly* what you asked for, but it does makes writing good code so much easier.
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: How to make PHP Code more Object Oriented

Post by Jackolantern »

Oh, don't know how I missed this one before.

"this" is a reference common in almost all OO languages which allows you to reference the object that the code is in. It seems rather vague or not very useful until you really get into OO, and then you realize you need it almost all the time and there is no substitute for it. In some languages like PHP, it is the only way to references class fields:

Code: Select all

$this->palacio
That means, inside this class, there is a variable (a class field) called "palacio". $this->palacio references it. Simply writing out $palacio won't work, because in PHP, that syntax can only reference "local variables", which are those variables that are passed into a method (a class function), or you created yourself inside of a method.
The indelible lord of tl;dr
alexrules01
Posts: 175
Joined: Sun Oct 11, 2009 9:33 am

Re: How to make PHP Code more Object Oriented

Post by alexrules01 »

I'm kind of like Vintinho, wanting to know how to make a website more OO. I understand its not always more efficient, but I believe, ecspecially coding games in PHP, using Class's would be much more efficient. Like, I get what OOP is. I know what classes are, methods and constructors, but once I sit down to code something for myself, I either code this super basic OO style, which seems like its using a procedural structure, but everything is within classes lol, or, I completely fail.
I'm hoping a course I'm taking next year for uni will help me with this problem. But I guess it is just the same old keep trying and you will eventually get it kinda thing lol.
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: How to make PHP Code more Object Oriented

Post by Jackolantern »

My best advice is that when people first start out with OOP, they feel like they need to create complex class hierarchies that use deep inheritance lineages. In reality, most applications will have only some classes inheriting at all, and it is usually just used to divide up a set of functionality between maybe 2 - 4 related classes.
The indelible lord of tl;dr
Post Reply

Return to “Advanced Help and Support”