Search
Left Quote    You have to THINK anyway, so why not think BIG?
- Donald Trump    
Right Quote
 
[login] | [Register]
 

functions, classes, and objects -- oh my!

by: misterhaan
Page: 3 of 5
(View All)

objects

we will start here by changing our two functions to actually write out the html rather than return it. this isn't neccessary in order to make this class into an object, but it gives us a reason to do something later:


now if we care about indentation so that the generated html is easy to read, we may want to write some spaces in front of what we are writing out from these two functions. since they belong to the same form, we can base their indentation off the same reference point. the option tag should be indented one level further than the input tag because option tags need to go inside select tags which would be at the same level as input tags. we will be using two spaces per level of indentation here, so let's add that to our option function:


i'm only showing the option function because that is all that changes. as i said before, we probably want to indent the whole form by some reference level. this is a good candidate for the FORM to remember between function calls, so let's introduce something new and add a variable to the FORM class. any variables you want to use for remembering information between function calls should go between the class line and the first function, and you specify them with 'var $varname' as follows:


notice that like our functions, $indent only exists within the FORM class. if we were still working with FORM as a static class, we could reference it as FORM::indent both inside and outside the class. this time we are going to use to FORM class to create an object, so we need to change the way we access the variables and functions we've defined inside the class. it is actually slightly different if the code we are writing is inside the class versus outside the class, so we will start with how to access our indent variable from inside the class.

i mentioned that we will be using our FORM class to create an object, which means we are going to assume that an object has already been created before anything inside the class is accessed (we will cover how to create objects later). when we make an object from the FORM class and then call one of the functions within the class, we get inside that object. to refer to the $indent that belongs to us, we use a special object called $this, which always refers to the object that we are currently inside. so instead of saying FORM we now will say $this. one other thing must change though, and that is the way we add on the name of the variable/function. a static reference starts with class name, then has :: and the variable/function name. an object reference from inside the class starts with $this, then has -> and the variable/function name. to access $indent from within input() we need to actually write $this->indent instead of just $indent:


i've added it to both functions here as it is the same for both.

now let's look at creating an object from this class. this is actually very easy. the following code would be somewhere outside of the FORM class and creates an object named $form from the FORM class we've defined:


all you need to do is say you want a new object from the FORM class, and assign that to your object variable. it is not actually important to use a name like $form when working with a class named FORM--we could have called it $x if we wanted, but $form is probably more descriptive. if you were actually going to use this, you could be even more descriptive and name it $signupform or something similar. it is also perfectly fine to create more than one object from the same class--each object will have its own copy of $indent in that case. if you created $form2 in the same way that we created $form, you could set $indent on one of them to 4 spaces and the other one could be set to 8 spaces.

since we now have an object, let's look at how to access the variables and functions that are inside it. we actually do this the same way as inside the class, except now we know the name of the object, so we use that instead of $this. for example, we probably want to set the value of $indent inside this form:


note that $form->indent only has meaning after we've created the form object from the FORM class by setting it to new FORM. the creation of the object is what gives us the context of the FORM class--again the input variable only exists in the context of FORM. we would similarly call our two functions by putting their names after $form-> -- just like we did with static calls when we put the names after FORM::. speaking of static calls, with our changes to add $this->indent to our functions, we can no longer call them statically without getting an error, as $this has no meaning unless we have an object.



1  |  2  |  3  |  4  |  5  |  
Next »


No Comments for this page.

You Must be logged in or a member to comment.


Tutorial Stats

Tutorial Stats

39,763 Views
1 Total Comments
4.6 Rating of 5 (5 Votes)

Options

Tutorial Options

· Login to Rate This Article
· Login to Post a Comment
· Read more by this author
Digg This Article! Del.icio.us: Bookmark This Article Reddit: Bookmark This Article BlinkList: Blink This Article! YahooMyWeb BlogMarks: Add This Mark! Furl: Save This Article Spurl: Mark This Article

Articles

Related    

· Simple PHP Tutorial
· One File Website
· Pagination with PHP
· Building a Comments Script
· Complete Members System
· Variable Scope



"AllSyntax.com" Copyright © 2002-2018; All rights lefted, all lefts righted.
Privacy Policy  |  Internet Rank