Jump to content

Everything in functions


orionlogic

Recommended Posts

Sorry for my noobish question but i want to talk this with a good programmer.

 

I am not a programmer but i wrote a web application.In this application there are users, user friends, posting articles etc. things like that.

Because i don't dive into class theory much, even though i understand what it is, i see creating functions is a faster way to accomplish my task.

 

For example when i want to get a users friends, i directly write a function for this; Basically querying DB & echo the outcome in HTML format directly from inside the function. Nearly every dynamically working part of the application follows this approach.

 

After writing 600 lines of functions, it becomes so boring.But it works, it is fast and its easy for me to understand.

 

Major drawback that i face is, i can't use most of the functions inside the other functions( because it echos HTML) so i wrote nearly identical functions to use inside for another one. A nightmare noobish approach :)

 

I want to ask what will i going to face later?  How can i refine my "only functions" design approach ?

Link to comment
https://forums.phpfreaks.com/topic/137173-everything-in-functions/
Share on other sites

first, i would start using classes to help organize things. a 'user' or 'person' class is the most common use of classes.

 

second, i RARELY have functions echo anything. they always return the value, and i echo it with the calling script. for example, i would have a 'user' class, with methods like getUsername(), getName(), getEmail(), etc. which return the appropriate values. i would also have a getFriends() method, which would return an array of user objects...one for each friend. then, have the calling script use echos/loops/etc to print the needed HTML.

 

if you are looking to condense and organize your HTML, i would look into Smarty. it's a PHP templating system. you can define a template for displaying a list of users. then have a template for a page of friends that calls that other template. it helps keep a 'standard' across the entire site and makes themes easy to create.

 

....hope that helps

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.