TalkPHP
 
 
Account Login
Latest Articles
» How to keep your forms from double posting data
» cURL Basics
» Securing your PHP applications Part 1
» The way the function rolls
» Database Abstraction with Zend_Db - Part 2
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Display Modes
Old 07-02-2008, 02:37 PM   #1 (permalink)
The Gregarious
Upcoming Programmer Inquisitive 
 
Join Date: Sep 2007
Posts: 540
Thanks: 64
Tanax is on a distinguished road
Default [Tutorial] Basic tutorial about class basics

Class basics

Hello, and welcome to this really basic tutorial. It's a basic tutorial, about the basics in classes. So I want to keep this as simple as possible so you can understand.

Let's begin with some words about classes.
Classes are a like a bunch of functions, that provides us with a set of options that we can use. So obviously, in order to understand this tutorial, you have to understand how functions works.
Classes are not a blueprint of what functions you need to use. Classes are only there to provide you with options that you CAN use.

Also, one thing worth mentioning, is that you need a somewhat new version of php-server in order for it to read the class correctly.
So with that said, let's check out the first thing you need to know when starting classes, which is ofcourse how to start the class.
php Code:
class cellphone {
       
       
       
    }

As you see, we just created a class called cellphone. This is how you create a class, it's THAT simple. Everything within those brackets will be within the class, and can only be used when you've called the class(which I will go through later).

So let's add some variables in the class. Let's say for example you want to store what color the cellphone has, in one variable. And what phonenumber it has, in another variable.

php Code:
class cellphone {
       
        private $phonenr;
        private $color;
       
    }

As you see, we don't set a value of these variables, we only put them out there. And also we used the PRIVATE option on those variables. I won't go through the different options you can put on variables(and functions!), because I think it's a little more advanced, so let's just keep the variables as private.

So go ahead and save this file as cellphone.php. Then go make a index.php where you include the cellphone.php.
index.php
php Code:
include('cellphone.php');
    $myphone = new cellphone();

As you see, we also create a cellphone! We assign an instance of the class cellphone, into the variable $myphone. So basicly, $myphone now contains everything within this class, which isn't anything basicly right now.

So let's go back to cellphone.php and create a function.
php Code:
class cellphone {
       
        private $phonenr;
        private $color;
       
        public function specs($nr, $color) {
           
            if(is_numeric($nr)) {
               
                $this->phonenr = $nr;
               
            }
           
            $this->color = $color;
           
        }
       
    }

Whoa! Now we had alot of stuff here. First of all, we created a function called specs, and we used PUBLIC option on this, because.. well, just because.
We also have $nr and $color in that function which we will set when calling the function. Inside the function we just check if the $nr is actually indeed a number, and if it is we assign its value into $this->phonenr.

So wait a sec.. what's $this-> ??? Well, because the class doesn't know if it's been called yet(created), we have to use $this to point that it's in THIS class we want to use something. So that's with everything within the class.
If you want to use a function in the class, from INSIDE the class, you have to use
PHP Code:
$this->functionname(); 
With that said, we can head back to index.php and set some specs, values to the phone we created.
php Code:
include('cellphone.php');
    $myphone = new cellphone();
    $myphone->specs(0704153359, 'white');

So, we've just set some values to the phone. And if you look at index.php in your browser, when the server is online ofcourse, it will show up as blank(if you've done correct). This is GOOD, because we haven't actually written out anything.
Let's go back to cellphone.php

We have to create a function that will retrieve the data from the phone.
php Code:
class cellphone {
       
        private $phonenr;
        private $color;
       
        public function specs($nr, $color) {
           
            if(is_numeric($nr)) {
               
                $this->phonenr = $nr;
               
            }
           
            $this->color = $color;
           
        }
       
        public function getdata() {
           
            if(isset($this->color) AND isset($this->phonenr)) {
               
                echo 'Your number is: '.$this->phonenr.'<br />Your cellphone is: '.$this->color;
               
            }
           
            else {
               
                echo 'One or more specs are missing! :O';
               
            }
           
        }
       
    }

So it's pretty basic here. We created the function getdata. We check if the variables are set and if they aren't, we write out that one or more is missing. If they both are set, we write out the values.
Now all we have to do is to call the function from index.php

php Code:
include('cellphone.php');
    $myphone = new cellphone();
    $myphone->specs(0704153359, 'white');
    $myphone->getdata();

Really simple.


So what's the advantage of this you ask?
Well, let's say you want to add another phone.

php Code:
include('cellphone.php');
    $myphone = new cellphone();
    $myphone->specs(0704153359, 'white');
    $myphone->getdata();

    echo '<br /><br />';
   
    $yourphone = new cellphone();
    $yourphone->specs(0735295832, 'blue');
    $yourphone->getdata();

That will echo out:

Code:
Your number is: 0704153359
Your cellphone is: white

Your number is: 0735295832
Your cellphone is: blue

Ofcourse, this is just basic stuff, and I'm almost certain that noone want to use a class to do this simple. But classes are very handy when for example creating a news system.
So this was all from me, I hope you learned something. And ask if you have any questions.
__________________

Last edited by Tanax : 07-13-2008 at 03:22 PM.
Tanax is offline  
Reply With Quote
The Following 4 Users Say Thank You to Tanax For This Useful Post:
codefreek (07-02-2008), Dave (07-03-2008), drewbee (07-09-2008), ETbyrne (07-09-2008)
Old 07-04-2008, 11:05 PM   #2 (permalink)
The Gregarious
Upcoming Programmer Inquisitive 
 
Join Date: Sep 2007
Posts: 540
Thanks: 64
Tanax is on a distinguished road
Default

Not one comment even? :(
__________________

Last edited by Tanax : 07-13-2008 at 03:21 PM.
Tanax is offline  
Reply With Quote
Old 07-05-2008, 12:12 PM   #3 (permalink)
The Addict
 
codefreek's Avatar
 
Join Date: Sep 2007
Location: Near you.
Posts: 283
Thanks: 166
codefreek is on a distinguished road
Default

Great tutorial, Tanax keep it up when is the next tutorial coming ?
__________________
inquisitive
1. Eager to acquire knowledge.
2. Too curious; overly interested; nosy.
codefreek is offline  
Reply With Quote
Old 07-08-2008, 11:30 AM   #4 (permalink)
The Contributor
 
quantumkangaroo's Avatar
 
Join Date: Feb 2008
Location: Pretoria, South Africa
Posts: 36
Thanks: 1
quantumkangaroo is an unknown quantity at this point
Default

Quote:
Originally Posted by Tanax View Post
Classes are a like a bunch of functions, that provides us with a set of options that we can use.
Classes are objects which contain methods, member variables, are able to be inherited and much more, objects make our life as developers easier by cutting down on repetitive code.

Quote:
Originally Posted by Tanax View Post
Also, one thing worth mentioning, is that you need a somewhat new version of php-server in order for it to read the class correctly.
Earlier versions of PHP support classes eg. PHP3/4, these are not somewhat new versions of php, object handling was rewritten in PHP5

Quote:
Originally Posted by Tanax View Post
php Code:
class cellphone {
       
        private $phonenr;
        private $color;
       
    }

As you see, we don't set a value of these variables, we only put them out there.
These variables are called class member variables and you may set a value to them when defining them.

Do a little more research before taking novice users into the world of object orientation?
__________________
virtueCart v1.0.5 developed by WebDevSA

Send a message via MSN to quantumkangaroo Send a message via Skype™ to quantumkangaroo
quantumkangaroo is offline  
Reply With Quote
Old 07-08-2008, 02:00 PM   #5 (permalink)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Cana'derr
Posts: 653
Thanks: 24
delayedinsanity is on a distinguished road
Default

Quote:
Classes are objects
No, classes are classes, objects are created from classes. They are however two different things.

Quote:
Earlier versions of PHP support classes eg.
Yes, but he was using keywords (such as public and private declarations) that are specific to PHP5's object handling, so to read his examples, you would need a newer release of PHP.

Quote:
These variables are called class member variables
Weeeeellllll, technically they're called properties.

Cut him a little slack, he may well have been doing the tutorial to learn as much for himself as to help others, and either way he didn't do a bad job. You are right though that everybody should do research before diving in, that's never bad advice.
-m
delayedinsanity is offline  
Reply With Quote
The Following 2 Users Say Thank You to delayedinsanity For This Useful Post:
codefreek (07-08-2008), Tanax (07-09-2008)
Old 07-08-2008, 04:08 PM   #6 (permalink)
The Gregarious
Upcoming Programmer Inquisitive 
 
Join Date: Sep 2007
Posts: 540
Thanks: 64
Tanax is on a distinguished road
Default

Quote:
Originally Posted by quantumkangaroo View Post
Classes are objects which contain methods, member variables, are able to be inherited and much more, objects make our life as developers easier by cutting down on repetitive code.
Like delayed said.
And it makes our life easier WITH a set of provided functions.

And actually I said "Classes are LIKE a set of...".
I didn't say it WAS a set of functions.


Quote:
Originally Posted by quantumkangaroo View Post
Earlier versions of PHP support classes eg. PHP3/4, these are not somewhat new versions of php, object handling was rewritten in PHP5
Indeed, but in earlier versions PRIVATE PUBLIC and PROTECTED didn't even exists. You used var $var; and for function you just declared it with function and no "private/public/protected".

In order to use this guide correctly, you need a somewhat newer version of PHP.


Quote:
Originally Posted by quantumkangaroo View Post
These variables are called class member variables and you may set a value to them when defining them.
No they're not called that. But I didn't set a value to them, now did I?
Thus, I said that we only put them there, WITH NO VALUE, because that's what I did.


Quote:
Originally Posted by quantumkangaroo View Post
Do a little more research before taking novice users into the world of object orientation?
Learn to read between the lines and actually take some time to understand what you're reading?
__________________

Last edited by Tanax : 07-13-2008 at 03:21 PM.
Tanax is offline  
Reply With Quote
The Following User Says Thank You to Tanax For This Useful Post:
codefreek (07-08-2008)
Old 07-09-2008, 01:52 AM   #7 (permalink)
The Wanderer
 
ICanFreelance's Avatar
 
Join Date: Jun 2008
Posts: 6
Thanks: 0
ICanFreelance is on a distinguished road
Default

Great article I will link to this so that other people will know there is a great article on Classes on the web.

Thanks
ICanFreelance is offline  
Reply With Quote
The Following User Says Thank You to ICanFreelance For This Useful Post:
Tanax (07-09-2008)
Old 07-09-2008, 01:41 PM   #8 (permalink)
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Default

Quote:
Originally Posted by delayedinsanity View Post
No, classes are classes, objects are created from classes. They are however two different things.
To be technical: An object is an instance of a class. One class can spawn many objects.
__________________
There are No Stupid Questions. But there a LOT of Inquisitive Idiots.
Send a message via AIM to drewbee
drewbee is offline  
Reply With Quote
Old 07-09-2008, 01:51 PM   #9 (permalink)
The Gregarious
Upcoming Programmer Inquisitive 
 
Join Date: Sep 2007
Posts: 540
Thanks: 64
Tanax is on a distinguished road
Default

Quote:
Originally Posted by ICanFreelance View Post
Great article I will link to this so that other people will know there is a great article on Classes on the web.

Thanks
Thanks alot mate! Glad you liked it

Quote:
Originally Posted by drewbee View Post
To be technical: An object is an instance of a class. One class can spawn many objects.
Omg, I can't believe I missed out to mention instance ahwell
__________________

Last edited by Tanax : 07-13-2008 at 03:21 PM.
Tanax is offline  
Reply With Quote
Old 07-09-2008, 01:59 PM   #10 (permalink)
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Default

lol. No worries. Good read anyways :)
__________________
There are No Stupid Questions. But there a LOT of Inquisitive Idiots.
Send a message via AIM to drewbee
drewbee is offline  
Reply With Quote
The Following User Says Thank You to drewbee For This Useful Post:
Tanax (07-09-2008)
Old 07-09-2008, 02:08 PM   #11 (permalink)
The Acquainted
 
ETbyrne's Avatar
 
Join Date: Dec 2007
Location: Lapeer, MI
Posts: 163
Thanks: 32
ETbyrne is on a distinguished road
Default

Good tutorial, thanks man.
__________________
My Website > http://www.evanbot.com
ETbyrne is offline  
Reply With Quote
The Following User Says Thank You to ETbyrne For This Useful Post:
Tanax (07-09-2008)
Old 07-09-2008, 02:19 PM   #12 (permalink)
The Gregarious
Upcoming Programmer Inquisitive 
 
Join Date: Sep 2007
Posts: 540
Thanks: 64
Tanax is on a distinguished road
Default

Quote:
Originally Posted by drewbee View Post
lol. No worries. Good read anyways :)
Hehe, thanks

Quote:
Originally Posted by ETbyrne View Post
Good tutorial, thanks man.
Thanks mate
__________________

Last edited by Tanax : 07-13-2008 at 03:20 PM.
Tanax is offline  
Reply With Quote
Old 07-23-2008, 03:37 PM   #13 (permalink)
The Visitor
 
Join Date: Jul 2008
Posts: 1
Thanks: 0
TNXT_Travis is on a distinguished road
Big Grin Great post!

This is a BASIC introduction people. Starter don't need to know exact terminology; that would be too much to ask. It got it's point across to me!

Awesome article! Can't wait for another!
__________________
There is no place like 192.168.1.1.
TNXT_Travis is offline  
Reply With Quote
Old 07-23-2008, 09:57 PM   #14 (permalink)
The Frequenter
Advanced Programmer Top Contributor Good Samaritan 
 
Join Date: Oct 2007
Location: Manchester, UK
Posts: 424
Thanks: 22
sketchMedia is on a distinguished road
Default

Nice work m8.
Quote:
These variables are called class member variables and you may set a value to them when defining them.
While this may be partly true, they are not bound to a specific name, many people call them object properties or just variables or even attributes, all are valid in my mind. There is nothing mystical about it, they are variables and related functions encapsulated within the instantiated object's (or class's if they are bound to the class like a static) namespace.
Quote:
Classes are objects which contain methods, member variables, are able to be inherited and much more, objects make our life as developers easier by cutting down on repetitive code.
Not to flog an already dying horse here but I think this point needs stressing, classes are anything but objects they are merely blueprints to an object, much like an architects plans to your house, hence why the 'new' keyword followed by the class name is used to create objects of that class type and the scope resolution operator '::' for static or function/variables bound to the class (i.e. not an object).

Whilst it may be good to be 'technical' and 'picky' about names of class/object functions/variables for a basic tutorial I hardly think its necessary and can be confusing for a new OOP'er to start abstracting names for functions (remember they are still declared using 'function') for a tutorial of this scope its unhelpful.


Aslong as the basic facts are underlined and understood by the reader its done its job.
__________________
sketchMedia is offline  
Reply With Quote
Old 07-24-2008, 01:37 PM   #15 (permalink)
The Gregarious
Upcoming Programmer Inquisitive 
 
Join Date: Sep 2007
Posts: 540
Thanks: 64
Tanax is on a distinguished road
Default

Quote:
Originally Posted by TNXT_Travis View Post
This is a BASIC introduction people. Starter don't need to know exact terminology; that would be too much to ask. It got it's point across to me!

Awesome article! Can't wait for another!
Thanks alot!

Quote:
Originally Posted by sketchMedia View Post
Nice work m8.
Thanks bro
__________________
Tanax is offline  
Reply With Quote
Reply