TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   [Tutorial] Basic tutorial about class basics (http://www.talkphp.com/absolute-beginners/3052-tutorial-basic-tutorial-about-class-basics.html)

Tanax 07-02-2008 02:37 PM

[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.

Tanax 07-04-2008 11:05 PM

Not one comment even? :(

codefreek 07-05-2008 12:12 PM

Great tutorial, Tanax keep it up when is the next tutorial coming ?

quantumkangaroo 07-08-2008 11:30 AM

Quote:

Originally Posted by Tanax (Post 16425)
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 (Post 16425)
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 (Post 16425)
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?

delayedinsanity 07-08-2008 02:00 PM

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

Tanax 07-08-2008 04:08 PM

Quote:

Originally Posted by quantumkangaroo (Post 16738)
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 (Post 16738)
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 (Post 16738)
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 (Post 16738)
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?

ICanFreelance 07-09-2008 01:52 AM

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

Thanks

drewbee 07-09-2008 01:41 PM

Quote:

Originally Posted by delayedinsanity (Post 16744)
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.

Tanax 07-09-2008 01:51 PM

Quote:

Originally Posted by ICanFreelance (Post 16771)
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 (Post 16785)
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 :-P

drewbee 07-09-2008 01:59 PM

lol. No worries. Good read anyways :)

ETbyrne 07-09-2008 02:08 PM

Good tutorial, thanks man. :-)

Tanax 07-09-2008 02:19 PM

Quote:

Originally Posted by drewbee (Post 16788)
lol. No worries. Good read anyways :)

Hehe, thanks ^^

Quote:

Originally Posted by ETbyrne (Post 16789)
Good tutorial, thanks man. :-)

Thanks mate :-D

TNXT_Travis 07-23-2008 03:37 PM

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!

sketchMedia 07-23-2008 09:57 PM

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.

Tanax 07-24-2008 01:37 PM

Quote:

Originally Posted by TNXT_Travis (Post 17306)
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 (Post 17312)
Nice work m8.

Thanks bro ;-)


All times are GMT. The time now is 07:56 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0