Current location: Hot Scripts Forums » Programming Languages » PHP » How to write code


How to write code

Reply
  #1 (permalink)  
Old 12-05-06, 06:13 AM
funny_shrshr funny_shrshr is offline
Newbie Coder
 
Join Date: Nov 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
Question How to write code

writing code and make more than one file ,each file has more than one function ...... when I see open sources i don't find all the code of page in the same page it has mathodologies which i don't know

do you have any refrence about how to write my code or any discussion about that...

thanks
Reply With Quote
  #2 (permalink)  
Old 12-05-06, 09:31 AM
odi's Avatar
odi odi is offline
Newbie Coder
 
Join Date: Oct 2006
Location: Switzerland
Posts: 79
Thanks: 0
Thanked 0 Times in 0 Posts
I don't know of such a reference, but I can tell you how I handle that:

Each functionality has it's own file. This doesn't mean that every function has it's own file, because there are functions from the same topic.
The main idea behind is to make your code clearer by bringing a file structure in it and to avoid too big files so you don't know what's inside.

Another purpose of using files is to use them more than once by including them whenever you need it (this can be content or code whatever).

I hope that helped a little bit.
Reply With Quote
  #3 (permalink)  
Old 12-05-06, 10:20 AM
nova912's Avatar
nova912 nova912 is offline
Code Guru
 
Join Date: Sep 2004
Location: Traverse City, MI, USA
Posts: 821
Thanks: 0
Thanked 0 Times in 0 Posts
Basically if it's something (a function/class) that can be adpated for more then one page make it a seperate file you can call on. When i write i make 1 file called "functions.php" just some small stuff i might have made up to help with small input filtering like..

safe() is a fuction i made up to filter incomming user input. I know i will use this on other parts of my site, so i add it to my functions.php file. That way i only need to write the function once. The only thing i need to do if i want access to my custom functions is put...
PHP Code:

require_once('includes/functions.php'); 

at the start of any document.

Now every function in that file can be called in the rest of the document.

When it comes to classes i make them just 1 file in this format: ClassName.class.php ("Validate.class.php" is a class i use a lot) You can include/require the class where you need it in your online applicaion.

The basic idea to to make your code clean and understandable to anyone as well as trying to make it as updatable from 1 source as possible. Anything that can be reused is a good thing, you dont want to have to change you database username and password in 56 document... just in 1 file that was "required_once()"'ed in those 56 documents.

The only code written in a document should be code that is responsible for what is proccessed when that document is loaded (e.g.: formatting the html, calling information from a database, Etc...)
__________________
"BTW, I can't program at all the only thing I figured out is how to upload templates to my server."
Reply With Quote
  #4 (permalink)  
Old 12-06-06, 01:27 AM
funny_shrshr funny_shrshr is offline
Newbie Coder
 
Join Date: Nov 2006
Posts: 29
Thanks: 0
Thanked 0 Times in 0 Posts
thanks odi and nova I'm already try to do that ...

I have an idea and want to know if it is right or not :
I make one file that have more than one function , each function draw a form and one filr have more than one function , each function contact with database ..... the two file are too big not in size but in command lines

another question ...
when i make a "function" and when i make a "class"

more one question
some people use a methodology that devide their code into layers(pages)... first layer is user interface , second security (which check if this user has permission to do this (like add,delete)) , third is layer which contact with data base with the operation the user ask for(like: add,delete ....)

I know thii is more useful for security and give permission to every one on the system , also to know the person who make any operation on database . I just know this mehtodology (I think I don't know it perfect ) but i hasn't implement it yet

also I made one file of type "javascript" which contain all functions that make validation for user input (client side) but I know that there is a validation on server side . I will be glade if I know how pleeeeeeez


thanks in advance
Reply With Quote
  #5 (permalink)  
Old 12-06-06, 05:22 AM
ThaLyric ThaLyric is offline
Newbie Coder
 
Join Date: Jul 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
make one file that have more than one function , each function draw a form and one filr have more than one function , each function contact with database ..... the two file are too big not in size but in command lines
I wouldn't do that. I would create seperate files wich contains those functions. For example, all database connection related functions are in database.php. All form functions are in forms.php. This way you can find your functions much faster and you cannot alter up any other functions by accident. So for example you're working on the database php file, you cannot alter the forms php file.

Quote:
when i make a "function" and when i make a "class"
Nowaydays I stuff everything in a class. Why? This way I can find my functions even faster.

Let's just say you have a Forms class. All the functions are in forms.class.php (already by the php file name I can see it's a class). So somewhere in your code you have something like

Code:
include("forms.class.php");
And when you want to use it:
Code:
$form=new Forms();
$form->doAFunction();
Now, this tells me, $form is a instance of the class Forms. So I must have a php file who has that class and in this case it is forms.class.php (I usually name the php file the same as the classname, but lowercased). So when I need to alter the function doAfunction() I just open forms.class.php

And even when you don't know wich file it was, just search all php files on

class Forms

and you'll get 1 hit (usually), because you can only have one class with the same name.

Also putting your functions in a class is also handy because of the inheritance (for example class B extends class A).

Last edited by ThaLyric; 12-06-06 at 05:27 AM.
Reply With Quote
  #6 (permalink)  
Old 12-06-06, 05:36 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
Quote:
Originally Posted by ThaLyric
Nowaydays I stuff everything in a class. Why? This way I can find my functions even faster.
So you're saying that the only reason you are building classes is because it's handy to find functions? with all respects, but that's just a stupid reason.

I build classes when i see a relation between the variables that i'm using on a simple page, and the functions that are executed on those variables.

Quote:
Originally Posted by wikipedia
A class is a cohesive package that consists of a particular kind of compile-time metadata. It describes the rules by which objects behave; these objects are referred to as "instances" of that class. A class specifies the structure of data which each instance contains as well as the methods (functions) which manipulate the data of the object and perform tasks; such methods are sometimes described as "behavior". A method is a function with a special property that it has access to data stored in an object. A class is the most specific type of an object in relation to a specific layer. A class may also have a representation (metaobject) at run-time, which provides run-time support for manipulating the class-related metadata.
check out this wikipedia link as well:
http://en.wikipedia.org/wiki/Class_%...ter_science%29

Enjoy programming classes,
UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #7 (permalink)  
Old 12-07-06, 02:41 AM
ThaLyric ThaLyric is offline
Newbie Coder
 
Join Date: Jul 2006
Posts: 15
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
So you're saying that the only reason you are building classes is because it's handy to find functions? with all respects, but that's just a stupid reason.
no ... if you read further I also mentioned the OOP part (like inheritence) and of course all the aspects that comes with OOP. Easy finding of the function is just one of the reasons. But of course it's the OOP thinking is the key factor. To bad not every host supports PHP 5(yes they are still out there ).

Last edited by ThaLyric; 12-07-06 at 02:43 AM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
for loop to retrieve info and write approbiate line of code? benq PHP 0 05-07-06 04:45 AM
SMS (Shared Short Code / keyword) vagabondos Job Offers & Assistance 0 11-04-05 09:01 PM
Write to new window with WMP embed code? Yamaha PHP 0 03-23-05 07:13 AM
How do i write a code to repeat rows with mutliple columns tundewoods PHP 7 09-03-04 05:16 AM
How to sale php code to customer without giving him code pradeep_soft PHP 4 03-12-04 12:10 PM


All times are GMT -5. The time now is 05:46 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.