Learn how to code your own Web Designs here! Learn how to code your own Web Designs here!Learn how to code your own Web Designs here!Learn how to code your own Web Designs here!Learn how to code your own Web Designs here!Learn how to code your own Web Designs here!Learn how to code your own Web Designs here!

Go Back   LayeredGFX Forums > Graphics Tutorials > Web Design Tutorials > Coding Tutorials
Register
Register Search Today's Posts Mark Forums Read

Text Link Ads

Object Oriented Programming


Coding Tutorials


Learn how to code your own Web Designs here!
This is a discussion on Object Oriented Programming within the Coding Tutorials, part of the Web Design Tutorials category; Article/Tutorial taken from my site, dot-silver.co.uk Well I've recently been getting the hang of OOP, which stands for Object-Oriented ...

Tags: , , , ,

 
 
LinkBack Thread Tools
  #1 (permalink)  
Old 08-15-2007, 02:59 PM
Onlooker
 
Object Oriented Programming

Article/Tutorial taken from my site, dot-silver.co.uk

Well I've recently been getting the hang of OOP, which stands for Object-Oriented Programming, so I thought I would share to others the basics of OOP. It may seem hard or confusing at first sight, but it's actually really easy to get a hand of. This tutorial will show the basics as well as say what OOP is and when it's mainly used.

OOP, which already said, stands for Object-Oriented Programming, if you're familiar with other programming languages such as Delphi or C++, etc. then you may know this as something like Procedures. It uses a class with functions inside of this class that can also connect to each other. So say you're using a variable in one function, that you've used in another function, then you can use it without having to define that variable again.

OOP is mainly used for when you're building large applications where you will be re-using data over again, making your large application easier to write and take a shorter time.

So let's start our class, we define a class using class with a name next to it.

PHP Code:
<?php
class MyClass{
}
?>


We've just made a new class, now inside of these classes, we have functions, so here's our first function.

PHP Code:
<?php
class MyClass{
    function 
MyClass()**
    }
}
?>
Don't worry, I'm not trying to confuse you, when you have a function that is the same name as the class, then this function will be ran automatically upon calling the new class. So let's add something into this function

[php<?php
class MyClass{
function MyClass()**
print 'Hello';
}
}[/php]

So when we call for this class, it will automatically run the function, MyClass and show the word Hello, so here's how we call a new class.

PHP Code:
<?php $myc = new MyClass?>
Simple right? We use a variable before calling the new class, this is so we can call anything from in that class. So in a full code.

PHP Code:
<?php
class MyClass{

    function 
MyClass()**
        print 
'Hello';
    }
}

$myc = new MyClass;
?>
Go on, test it. But as I said just a moment ago, we use a variable when calling the class because that will define everything that is in the class. For example, we use this variable to call function. Here's how.

PHP Code:
<?php
class MyClass{
    function 
MyClass()**
        print 
'Hello';
    }
}

$myc = new MyClass;
$myc->MyClass();
?>
This will now print the word, Hello, twice.
But we can do a lot more than just this, what about a user defined input?

PHP Code:
<?php
class MyClass{
    function 
Input($input)**
        print 
$input;
    }
}

$myc = new MyClass;
$myc->Input('Hello');
?>
Something else that's good about OOP, you can define variables, so here's one now using the same as the above example code.

PHP Code:
<?php
class MyClass{
    var 
$input 'No Text';
    function 
Input()**
        print 
$this->input;
    }
}

$myc = new MyClass;
$myc->Input();
?>
So we define a variable using var and make it set a value in it that says "No Text". We take away the $input from the function, and change the $input variable to $this->input. $this signifies it's going towards the classes own variable. We can change this in the php itself. Here's how.

PHP Code:
<?php
class MyClass{
    var 
$input 'No Text';
    function 
Input()**
        print 
$this->input;
    }
}

$myc = new MyClass;
$myc->input 'My Text';
$myc->Input();
?>
We use $myc->input, all functions and variables are capital sensitive, so it sees $myc, referring into the class, the variable known as input, now equals this.

Now let's get onto something else, using two functions inside a class.

PHP Code:
<?php

class MyClass{

    var 
$value 'No Value';

    function 
Show()**

        print 
$this->value;

    }

    function 
Change($value)**

        
$this->value $value;

    }

}

print 
'<strong>Value: </strong>';

$myc = new MyClass;

$myc->Show();

?>
So we've changed the variable to $value which equals "No Value", we've changed Input with Show(), and added a new function called Change($value), we'll start with Change($value) first. What this does is when it's called upon, it will change the value in $value in the class.

The Show() will show what ever is in the class $value variable. So at this moment, it will show No Value. We can easily change this though. By adding

PHP Code:
$myc->Change('5'); 
PHP Code:
<?php

class MyClass{

    var 
$value 'No Value';

    function 
Show()**

        print 
$this->value;

    }

    function 
Change($value)**

        
$this->value $value;

    }

}

print 
'<strong>Value: </strong>';

$myc = new MyClass;

$myc->Change('5');

$myc->Show();

?>
Ok now we're going to introduce a form into this so a user can change the value themselves. We'll let them choose a value of 1-10. So here's what we do.

- Keep the same class that we already have.
- Print the first part of the form
- Use PHP to count 1-10 easily for us using a while loop.
- Finish off the form
- Make the new class
- Has the form been submitted?
- If so change the value using OOP
- Show the value.

PHP Code:
<?php

    
class MyClass{

        var 
$value 'No Value';

        function 
Show()**

            print 
$this->value;

        }

        function 
Change($value)**

            
$this->value $value;

        }

    }

    

    print 
'<form method="post">

    <select name="value">'
;

    
$i 1;

    while(
$i 11)**

        print 
'<option value="'.$i.'">'.$i.'</option>';

        
$i++;

    }

    print 
'</select>

    <input type="submit">

    </form><br /><br />'
;

    
$myc = new MyClass;

    if(
$v $_POST['value'])**

        
$myc->Change($v);

    }

    print 
'<strong>Value: </strong>';

    
$myc->Show();

?>
Read through it carefully to understand it. Its basically what we did before but changing it from a user input from a form field. If you are unsure how to form fields, then read PHP Form Processing.

That's it, if you have any questions, do not hesitate to ask.
__________________
 


Thread Tools
Display Modes




All times are GMT. The time now is 08:27 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO 3.1.0
Template-Modifikationen durch TMS
vBCredits v1.3 ©2007 by Darkwaltz4