![]() |
Writing your first class
A mate of mine asked me today how do you write a class so i decided after giving him an explanation to make a tutorial about it. Ok so your used to the php language now right? So whats the hype about classes and functions. Lets say you are writing a large application, wouldn't it be nice if you could use the same code over and over and over and over again? It is possible, functions and classes make our lives as programmers so much easier. For this tutorial we are going to write a basic calculator, basic meaning it will only have 2 input fields and a button.
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
The Scope Resolution Operator or double colon :: allows us access to methods of a class. It is used in object-oriented programming when you want to be specific about what kind of function you are calling. The most common use for scope resolution is with the pseudo-class parent. For example, if you want a child object to call its parent's __construct() function, you would use parent::__construct(). Class::Function($argument1, $argument2); Now we can echo the answers to the user. Last but not least our html form. Here we show our input boxes and submit button. Remember there is a difference between using the POST and GET methods on a form, the GET method sends the values using the http header. PHP Code:
|
Its a nice "start", but you left out some major information when it comes to creating a class.
1. You didn't explain what "var" is, and how it relates to PHP5 and how the new standard for PHP5 works. 2. You didn't explain what an argument is, if I was a beginner I would be the first to ask what exactly a argument or parameter was. 3. You forgot to explain how "::" the scope worked when it came to classes. You might of "showed" us but you still didn't get into detail with how it worked. |
This was just a quick tutorial, ive added some detail. Enjoy
|
Just a note. Instead of doing this...
PHP Code:
PHP Code:
|
Yes that is another way of doing it
|
Nice tutorials but you forgot a lot of this. You didn't set a level for the function (public, private, protected). And if you want to call a function static you need to define it in de class, for example:
public static function Add(). You define 2 vars in your class but never uses them. |
There was really no need to set a level for the function i explained what the visibility keywords do, but i did not implement public, protected or private that doesnt mean i forgot about them there is no need for beginners to use these as of yet? If you want i can write an article about visibility keywords in the advanced programming section?
Quote:
|
I would appreciate a tutorial in the advanced programming section :). About the function levels, sorry about that, I read over it I think, my fault.
|
No problem its just to keep it as simple as possible for beginners, not giving too much detail as they would generally become confused, as for the advanced programming one let me finish my work then see what i can put together.
|
| All times are GMT. The time now is 09:43 AM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0