Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
Save to My Library
Look up keyword
Like this
0 of .
Results for:
No results containing your search query
P. 1
Beginning Object Oriented Programming in PHP

Beginning Object Oriented Programming in PHP



|Views: 982 |Likes:
Published by Abhilash V Pillai
It's a beginners tutorial for object oriented programming in PHP. It provides some basic code snippets and some basic ideas which will help you to get started in object oriented programming.
It's a beginners tutorial for object oriented programming in PHP. It provides some basic code snippets and some basic ideas which will help you to get started in object oriented programming.

More info:

Published by: Abhilash V Pillai on Jan 24, 2009
Copyright:Attribution Non-commercial


Read on Scribd mobile: iPhone, iPad and Android.
download as DOC, PDF, TXT or read online from Scribd
See more
See less





Beginning Object Oriented Programming inPHP
In this tutorial you will explore OOP in a way that'll start you on the fast track to polished OOP skills.Object-Oriented Programming (OOP) tutorials are generally bogged down with programming theoryand large metaphysical words such as encapsulation, inheritance and abstraction. They attempt toexplain things by comparing code samples to microwaves or automobiles, which only serves toconfuse the reader more.But even when all the hype and mystique that surrounds OOP has been stripped away, you'll find it isindeed a good thing. I won't list reasons why... I don't want this to be another cookie-cutter tutorial thatonly serves to confuse you. Instead, we'll explore OOP in a way that'll start you on the fast track to polished OOP skills. As you grow in confidence with your skills and begin to use them more in your  projects, you'll most likely form your own list of benefits.
Beginning Object Oriented Programming inPHP - Objects - The Magic Box
Imagine a box. It can be any type of box you want: a small jewelry box, a large crate, wooden, plastic,tall and thin, Short and wide... Next, imagine yourself placing something inside the box: a rock, a million dollars, your younger sibling...Wouldn't it be convenient if we could walk up to the box and just ask it to tell us what's inside insteadof opening it ourselves? Actually, we can!
<?php$mybox= newBox("Jack"); echo$mybox->get_whats_inside(); ?>
Here the variable $mybox represents our self-aware box (also known as an object) which will be built by new--the world's smallest engineering and construction team. We also want to place Jack inside the box when it is built. When we want to ask the box it's contents, we'll apply a special get_whats_insidefunction to $mybox.The code won't run quite yet, though. We haven't supplied new with the directions for him and histeam to construct our box and PHP doesn't know what the function get_whats_inside is supposed todo.
Beginning Object Oriented Programming inPHP - Classes
A class is technically defined as a representation of an abstract data type. In laymen's terms a class is a blueprint from which new will construct our box. It's made up of variables and functions that allowour box to be self-aware. With the blueprint, new can build our box exactly to our specifications.
<?phpclassBox{var$contents;functionBox($contents) {  $this->contents=$contents; }functionget_whats_inside() {return$this->contents; }}$mybox= newBox("Jack"); echo$mybox->get_whats_inside(); ?>
A closer look at our blueprint shows it contains the variable $contents which is used to remember thecontents of the box. It also contains two functions: Box and get_whats_inside.When the box springs into existence, PHP will look for and execute the function with the same nameas the class. That's why our first function has the same name as the class itself. The whole purpose of the Box function is to initialize the contents of the box.The special variable $this is used to tell Box that contents is a variable that belongs to the whole classand not the function itself. The $contents variable only exists within the scope of the function Box.$this->contents is a variable which was defined as part of the overall class.The function get_whats_inside returns the value stored in the class' contents variable, $this->contents.When the entire script is executed the class Box is defined, new constructs a box and passes "Jack" toit's startup function. The initialization function, which has the same name as the class itself, acceptsthe value passed to it and stores it within the class's variable so that it's accessible to functionsthroughout the entire class. Now we've got a nice, shiny new Jack in the Box.
Beginning Object Oriented Programming inPHP - Methods
To ask the box what it contains, the special get_whats_inside function was used. Functions defined inthe class are known as methods; they act as a method for communicating with and manipulating thedata within the box.The nice thing about methods is that they allow us to separate all the class coding from our actualscript.
We could save all of the class code in a separate file and then use include to import it into our script.Our scripts become more streamlined and, because we used descriptive names for our methods,anyone else reading our code can easily see our train-of-thought.
<?phpinclude("class.Box.php");$mybox= newBox("Jack"); echo$mybox->get_whats_inside(); ?>?>
Another benefit of methods is that it provides our box or whatever other objects we may build with astandard interface that anyone can use. We can share our classes with other programmers or evenimport them into our other scripts when the functionality they provide is needed.
<?phpinclude("class.Box.php");$mybox= newBox("Suggestion"); echo$mybox->get_whats_inside(); ?><?phpinclude("class.Box.php");$mybox= newBox("Shoes"); echo$mybox->get_whats_inside(); ?>
Beginning Object Oriented Programming inPHP - Extends
So far our example is only capable of telling us what its content is. We could add more methods to our class, but what if we were building extensions to someone else's class? We don't have to create anentirely new class to add new functionality... we can build a small extension class based on theoriginal.
<?php;include("class.Box.php");classShoeBoxextendsBox { varr $size;functionShoeBox($contents,$size) {  $this->contents=$contents;  $this->size=$size; }functionget_shoe_size() {return$this->size; }}

Activity (11)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
muniiera liked this
108-d liked this
108-d liked this
Nikunj Soni liked this
lafatwolf liked this
ti2pb4rk4h liked this

You're Reading a Free Preview

/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->