Post: Learn Object-oriented programming PHP
10-23-2010, 12:46 PM #1
(adsbygoogle = window.adsbygoogle || []).push({}); This won't be in-depth but rather a learners guide to learning OOP.

First let me describe what OOP (Object Priented Programming) is. This bit is copied from Wikipedia, so no credit to me


Originally posted by another user

Object-oriented programming (OOP) is a programming paradigm that uses "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. Programming techniques may include features such as data abstraction, encapsulation, modularity, polymorphism, and inheritance. It was not commonly used in mainstream software application development until the early 1990s.Many modern programming languages now support OOP.


That bit was copied from Wikipedia, now it's all me.

Right, OK. Now we're going to get on with the basics. I myself am not 'professional' so it's won't be inch-prefect, but it will do the job. And will be better than normal PHP.

Okay, so let's start our class. To start a class, you must put "class" then your class name. Minus the speech marks.



    class Tutorial{ 

}


Inside that class we can declare variables. And functions. To declare a variable we would do the following:



    class Tutorial{ 

var $myvariable;

}


Now we've done the above we can call that variable like so:


    $this->myvariable(); 


Okay, now let's get onto functions. We can use a function inside or outside a class. But for this we're going to use it inside. To use a function we can use three types of 'protection' These are:

Private
Protected
Public

Not in any order. You don't have to use any of the above. Okay so let's use our function to do something:

    class Tutorial{ 

var $myvariable;

function sayhi($reply){
echo 'Hello there<br/>';
echo $reply;

}
}


Okay, to use this function we would do the following in the same page or on an external page:



    $myvar = new Tutorial; 
$myvar->sayhi('Hello there'Winky Winky;


Right, now we know that, let's get on to other parts. Let's say we wanted to create a 'Framework' and we had our layout like so:


include
system
applications


In the applications we would put out mods. So this is were this other code comes into play.

We have a mod that needs to connect to the database, or libaries etc. Well we would create a class called Database and inside that we would use a function called:

    __construct


So if we used that is one of our classes, we could create another page and create a class like so, which will inherit that other class that used

     __construct

:


    class myfirstmod extends database{ 

/**
Now we can run quiries without having to connect to the
database in this file because the parent class has
already connected.
**/

}


That handy feature can be used in alot more ways.


A parent database class would look something like so:

    
<?php


class Database
{

var $sel;
var $con;

function __construct()
{
$this->connect();
}

function connect()
{
$this->con = mysql_connect("localhost", 'root', ''Winky Winky or die(mysql_error());
$this->sel = mysql_select_db('dbname'Winky Winky or die(mysql_error());
}
}
$database = new Database;
?>
(adsbygoogle = window.adsbygoogle || []).push({});

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo