SlideShare a Scribd company logo
Beginning Object Oriented
          Programming in PHP
                                   by Timothy Boronczyk
                                             2003-11-11


     Synopsis
     In this tutorial you will explore OOP in a way that'll start you on the fast track to
     polished OOP skills.




http://codewalkers.com/tutorials/54/1.html                                                   Page 1
Beginning Object Oriented Programming in PHP
                                                                           by Timothy Boronczyk




     Introduction
     Object-Oriented Programming tutorials are generally bogged down with programming
     theory and large, metaphysical words such as encapsulation, inheritance and
     abstraction. They attempt to explain things by comparing code samples to microwaves
     or automobiles which only serves to confuse the reader more.
     But even when all the hype and mystique that surrounds Object-Oriented Programming
     (OOP) has been stripped away, you'll find it is indeed a good thing. I won't list reasons
     why; this isn't another cookie-cutter tutorial only serving 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 OOP skills and begin to use them more in your projects,
     you'll most likely form your own list of benefits.

     The Magic Box (Objects)
     Imagine a box. It can be any type of box you want... a small jewelery 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...
     Now wouldn't it be convenient if we could walk up to the box and just ask it to tell us
     what's inside instead of opening it ourselves? Actually, we can!

     <?php

       $mybox = new Box("Jack");
       echo $mybox->get_whats_inside();

     ?>

     Here the variable $mybox represents our self-aware box, which is 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 function to $mybox, get_whats_inside().
     But the code won't run quite yet, though; we haven't supplied new with the directions
     for him and his team to construct our box and PHP doesn't know what the function
     get_whats_inside() is supposed to do.

     Classes
     A class is technically defined as a representation of an abstract data type. In laymen's
     term, a class is a blueprint from which new will construct our box. It's made up of
     variables and functions which allow our box to be self-aware. With the blueprint, new
     can now builds our box exactly to our specifications.



http://codewalkers.com/tutorials/54/1.html                                              Page 2
Beginning Object Oriented Programming in PHP
                                                                              by Timothy Boronczyk


     <?php

       class Box
       {
         var $contents;

           function Box($contents) {
             $this->contents = $contents;
           }

           function get_whats_inside() {
             return $this->contents;
           }
       }

       $mybox = new Box("Jack");
       echo $mybox->get_whats_inside();

     ?>

     Let's take a closer look at our blueprint: it contains the variable $contents which is used
     to remember the contents 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 name as the class. That's why our first function has the same name as the
     class itself. And if we look closer still, we'll notice the whole purpose of the Box()
     function is to initialize the contents of the box.
     $this is used to tell Box() that contents is a varable that belongs to the whole class, not
     the function itself. $contents is a variable which only exists within the scope of the
     function Box(). $this->contents is a variable which was defined by 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" to it's initialization function. The initialization function, which has the
     same name as the class itself, accepts the value passed to it and stores it within the
     class's variable so that it's accessable to functions throughout the entire class.
     Now we've got a nice, brand new Jack in the Box (yes, I've been waiting the entire
     tutorial to say that).

     Methods
     To ask the box what it contains, the special get_whats_inside() function was used. The
     functions defined in the class are known as methods; they act as a method for
     communicating with and manipulating the data within the box.
     The nice thing about methods is that they allow us to separate all the class coding from
     our actual script.

http://codewalkers.com/tutorials/54/1.html                                                Page 3
Beginning Object Oriented Programming in PHP
                                                                            by Timothy Boronczyk


     <?php

       include("class.Box.php");

       $mybox = new Box("Jack");
       echo $mybox->get_whats_inside();

     ?>

     We could save all of the class code in a separate file (in this case I've named the file
     class.Box.php) 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.
     Another benefit of methods is that it provides our box, or whatever other objects we
     may build, with a standard interface that anyone can use. We can share our classes
     with other programmers or even import them into our other scripts where the
     functionality they provide is needed.

     <?php

       include("class.Box.php");

       $mybox = new Box("Suggestion");
       echo $mybox->get_whats_inside();

     ?>


     <?php

       include("class.Box.php");

       $mybox = new Box("Shoes");
       echo $mybox->get_whats_inside();

     ?>


     Extends
     A smart box is a wonderful object to have, but so far it's only capable of telling us what
     its content is. We don't have create an entirely new class to add new functionality...
     instead, we can build a small extention class based on the original.

     <?php

       include("class.Box.php");

       class ShoeBox extends Box
       {


http://codewalkers.com/tutorials/54/1.html                                               Page 4
Beginning Object Oriented Programming in PHP
                                                                           by Timothy Boronczyk


           var $size;

           function ShoeBox($contents, $size) {
             $this->contents = $contents;
             $this->size = $size;
           }

           function get_shoe_size() {
             return $this->size;
           }
       }

       $mybox = new ShoeBox("Shoes", 10);
       echo $mybox->get_whats_inside();
       echo $mybox->get_shoe_size();

     ?>

     With the extends keyword, our script now has a ShoeBox class based on our original
     Box class. ShoeBox has the same functionality as Box class, but with extra functions
     specific to a special kind of box.
     The ability to write such modular addons to your code gives great flexability in testing
     out new functions and saves time by reusing the same core code.

     <?php

       include("class.Box.php");
       include("extentions.Shoe.php");
       include("extentions.Suggestion.php");
       include("extentions.Cardboard.php");

       $mybox = new ShoeBox("Shoes", 10);
       $mySuggestion = new SuggestionBox("Complaints");
       $myCardboard = new CardboardBox('', "corrugated", "18in",
     "12in", "10in");

     ?>


     Conclusion
     You can see now how Object Oriented Programming got it's name--by focusing on
     building programs as a set of self-aware or smart objects.
     The ability to design modular code which OOP practices afford will help you save time,
     reduce stress and easily share your work with others. It isn't necessarily difficult, but
     rather it's the technical and philosophical jargon associated with it that can cause
     confusion for beginners. But, with perseverance and practice, your understanding of
     will grow... as so will your confidence!
     About the Author

http://codewalkers.com/tutorials/54/1.html                                              Page 5
Beginning Object Oriented Programming in PHP
                                                                        by Timothy Boronczyk




     Timothy Boronczyk lives in Syracuse, NY, where he works as an E-Services
     Coordinator for a local credit union. He has a background in elementary education,
     over 5 years experience in web design and has written tutorials on web design, PHP,
     Ruby, XML and various other topics. His hobbies include photography and composing
     music.




http://codewalkers.com/tutorials/54/1.html                                           Page 6

More Related Content

What's hot (17)

PPTX
Object oriented programming in php 5
Sayed Ahmed
 
PPT
Oops in PHP
Mindfire Solutions
 
PDF
OOP in PHP
Alena Holligan
 
PPTX
Object oreinted php | OOPs
Ravi Bhadauria
 
ZIP
Object Oriented PHP5
Jason Austin
 
PPTX
Introduction to PHP OOP
fakhrul hasan
 
PPTX
Basics of Object Oriented Programming in Python
Sujith Kumar
 
PDF
A Gentle Introduction To Object Oriented Php
Michael Girouard
 
DOCX
Oops concept in php
selvabalaji k
 
PDF
Intermediate OOP in PHP
David Stockton
 
PPT
Oops concepts in php
CPD INDIA
 
PDF
Dependency Injection Why is it awesome and Why should I care?
ColdFusionConference
 
PPT
Basic Oops concept of PHP
Rohan Sharma
 
PPT
Introduction to OOP with PHP
Michael Peacock
 
PPT
Introduction Php
sanjay joshi
 
PPTX
Php oop presentation
Mutinda Boniface
 
Object oriented programming in php 5
Sayed Ahmed
 
Oops in PHP
Mindfire Solutions
 
OOP in PHP
Alena Holligan
 
Object oreinted php | OOPs
Ravi Bhadauria
 
Object Oriented PHP5
Jason Austin
 
Introduction to PHP OOP
fakhrul hasan
 
Basics of Object Oriented Programming in Python
Sujith Kumar
 
A Gentle Introduction To Object Oriented Php
Michael Girouard
 
Oops concept in php
selvabalaji k
 
Intermediate OOP in PHP
David Stockton
 
Oops concepts in php
CPD INDIA
 
Dependency Injection Why is it awesome and Why should I care?
ColdFusionConference
 
Basic Oops concept of PHP
Rohan Sharma
 
Introduction to OOP with PHP
Michael Peacock
 
Introduction Php
sanjay joshi
 
Php oop presentation
Mutinda Boniface
 

Viewers also liked (6)

PDF
Psychologia Osiagniec
Halik990
 
PDF
caseywest
tutorialsruby
 
PDF
ACM HPDC 2010参加報告
Ryousei Takano
 
PDF
xv6のコンテキストスイッチを読む
mfumi
 
PDF
xv6から始めるSPIN入門
Ryousei Takano
 
PDF
Toward a practical “HPC Cloud”: Performance tuning of a virtualized HPC cluster
Ryousei Takano
 
Psychologia Osiagniec
Halik990
 
caseywest
tutorialsruby
 
ACM HPDC 2010参加報告
Ryousei Takano
 
xv6のコンテキストスイッチを読む
mfumi
 
xv6から始めるSPIN入門
Ryousei Takano
 
Toward a practical “HPC Cloud”: Performance tuning of a virtualized HPC cluster
Ryousei Takano
 
Ad

Similar to tutorial54 (20)

PDF
Oop in php tutorial
Gua Syed Al Yahya
 
PDF
Oop in php_tutorial_for_killerphp.com
ayandoesnotemail
 
PDF
oop_in_php_tutorial_for_killerphp.com
tutorialsruby
 
PDF
Oop in php_tutorial
Gregory Hanis
 
PPTX
Php oop (1)
Sudip Simkhada
 
PDF
Design patterns
Jason Austin
 
PPTX
Constructor and encapsulation in php
SHIVANI SONI
 
PDF
Oop basic concepts
Swarup Kumar Boro
 
PPTX
Lecture-10_PHP-OOP.pptx
ShaownRoy1
 
PDF
Object And Oriented Programing ( Oop ) Languages
Jessica Deakin
 
PDF
Phparchitect Box Of Php Eric Mann Eric Van Johnson Chris Tankersley
qashtutjahjo
 
PDF
From Java to Python: beating the Stockholm syndrome
Javier Arias Losada
 
PPTX
Copy is a 4 letter word
Malcolm Murray
 
PDF
Ad507
Bill Buchan
 
PDF
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Bill Buchan
 
PPTX
Object oriented javascript
Usman Mehmood
 
PDF
Embrace dynamic PHP
Paul Houle
 
PDF
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Bill Buchan
 
PPTX
PHP OOP Lecture - 01.pptx
Atikur Rahman
 
PPTX
Object-oriented programming 3.pptx
Adikhan27
 
Oop in php tutorial
Gua Syed Al Yahya
 
Oop in php_tutorial_for_killerphp.com
ayandoesnotemail
 
oop_in_php_tutorial_for_killerphp.com
tutorialsruby
 
Oop in php_tutorial
Gregory Hanis
 
Php oop (1)
Sudip Simkhada
 
Design patterns
Jason Austin
 
Constructor and encapsulation in php
SHIVANI SONI
 
Oop basic concepts
Swarup Kumar Boro
 
Lecture-10_PHP-OOP.pptx
ShaownRoy1
 
Object And Oriented Programing ( Oop ) Languages
Jessica Deakin
 
Phparchitect Box Of Php Eric Mann Eric Van Johnson Chris Tankersley
qashtutjahjo
 
From Java to Python: beating the Stockholm syndrome
Javier Arias Losada
 
Copy is a 4 letter word
Malcolm Murray
 
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Bill Buchan
 
Object oriented javascript
Usman Mehmood
 
Embrace dynamic PHP
Paul Houle
 
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Bill Buchan
 
PHP OOP Lecture - 01.pptx
Atikur Rahman
 
Object-oriented programming 3.pptx
Adikhan27
 
Ad

More from tutorialsruby (20)

PDF
&lt;img src="../i/r_14.png" />
tutorialsruby
 
PDF
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
tutorialsruby
 
PDF
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
tutorialsruby
 
PDF
&lt;img src="../i/r_14.png" />
tutorialsruby
 
PDF
&lt;img src="../i/r_14.png" />
tutorialsruby
 
PDF
Standardization and Knowledge Transfer – INS0
tutorialsruby
 
PDF
xhtml_basics
tutorialsruby
 
PDF
xhtml_basics
tutorialsruby
 
PDF
xhtml-documentation
tutorialsruby
 
PDF
xhtml-documentation
tutorialsruby
 
PDF
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
tutorialsruby
 
PDF
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
tutorialsruby
 
PDF
HowTo_CSS
tutorialsruby
 
PDF
HowTo_CSS
tutorialsruby
 
PDF
BloggingWithStyle_2008
tutorialsruby
 
PDF
BloggingWithStyle_2008
tutorialsruby
 
PDF
cascadingstylesheets
tutorialsruby
 
PDF
cascadingstylesheets
tutorialsruby
 
&lt;img src="../i/r_14.png" />
tutorialsruby
 
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
tutorialsruby
 
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
tutorialsruby
 
&lt;img src="../i/r_14.png" />
tutorialsruby
 
&lt;img src="../i/r_14.png" />
tutorialsruby
 
Standardization and Knowledge Transfer – INS0
tutorialsruby
 
xhtml_basics
tutorialsruby
 
xhtml_basics
tutorialsruby
 
xhtml-documentation
tutorialsruby
 
xhtml-documentation
tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
tutorialsruby
 
HowTo_CSS
tutorialsruby
 
HowTo_CSS
tutorialsruby
 
BloggingWithStyle_2008
tutorialsruby
 
BloggingWithStyle_2008
tutorialsruby
 
cascadingstylesheets
tutorialsruby
 
cascadingstylesheets
tutorialsruby
 

Recently uploaded (20)

PPTX
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
PPTX
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PDF
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
PDF
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
The Future of Artificial Intelligence (AI)
Mukul
 
PPTX
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
PDF
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
PDF
Brief History of Internet - Early Days of Internet
sutharharshit158
 
PDF
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
PDF
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PDF
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
PDF
Researching The Best Chat SDK Providers in 2025
Ray Fields
 
Agile Chennai 18-19 July 2025 | Emerging patterns in Agentic AI by Bharani Su...
AgileNetwork
 
AI Code Generation Risks (Ramkumar Dilli, CIO, Myridius)
Priyanka Aash
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
Research-Fundamentals-and-Topic-Development.pdf
ayesha butalia
 
Build with AI and GDG Cloud Bydgoszcz- ADK .pdf
jaroslawgajewski1
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
How Open Source Changed My Career by abdelrahman ismail
a0m0rajab1
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
The Future of Artificial Intelligence (AI)
Mukul
 
Farrell_Programming Logic and Design slides_10e_ch02_PowerPoint.pptx
bashnahara11
 
NewMind AI Weekly Chronicles – July’25, Week III
NewMind AI
 
Brief History of Internet - Early Days of Internet
sutharharshit158
 
CIFDAQ's Market Wrap : Bears Back in Control?
CIFDAQ
 
TrustArc Webinar - Navigating Data Privacy in LATAM: Laws, Trends, and Compli...
TrustArc
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
AI Unleashed - Shaping the Future -Starting Today - AIOUG Yatra 2025 - For Co...
Sandesh Rao
 
Researching The Best Chat SDK Providers in 2025
Ray Fields
 

tutorial54

  • 1. Beginning Object Oriented Programming in PHP by Timothy Boronczyk 2003-11-11 Synopsis In this tutorial you will explore OOP in a way that'll start you on the fast track to polished OOP skills. http://codewalkers.com/tutorials/54/1.html Page 1
  • 2. Beginning Object Oriented Programming in PHP by Timothy Boronczyk Introduction Object-Oriented Programming tutorials are generally bogged down with programming theory and large, metaphysical words such as encapsulation, inheritance and abstraction. They attempt to explain things by comparing code samples to microwaves or automobiles which only serves to confuse the reader more. But even when all the hype and mystique that surrounds Object-Oriented Programming (OOP) has been stripped away, you'll find it is indeed a good thing. I won't list reasons why; this isn't another cookie-cutter tutorial only serving 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 OOP skills and begin to use them more in your projects, you'll most likely form your own list of benefits. The Magic Box (Objects) Imagine a box. It can be any type of box you want... a small jewelery 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... Now wouldn't it be convenient if we could walk up to the box and just ask it to tell us what's inside instead of opening it ourselves? Actually, we can! <?php $mybox = new Box("Jack"); echo $mybox->get_whats_inside(); ?> Here the variable $mybox represents our self-aware box, which is 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 function to $mybox, get_whats_inside(). But the code won't run quite yet, though; we haven't supplied new with the directions for him and his team to construct our box and PHP doesn't know what the function get_whats_inside() is supposed to do. Classes A class is technically defined as a representation of an abstract data type. In laymen's term, a class is a blueprint from which new will construct our box. It's made up of variables and functions which allow our box to be self-aware. With the blueprint, new can now builds our box exactly to our specifications. http://codewalkers.com/tutorials/54/1.html Page 2
  • 3. Beginning Object Oriented Programming in PHP by Timothy Boronczyk <?php class Box { var $contents; function Box($contents) { $this->contents = $contents; } function get_whats_inside() { return $this->contents; } } $mybox = new Box("Jack"); echo $mybox->get_whats_inside(); ?> Let's take a closer look at our blueprint: it contains the variable $contents which is used to remember the contents 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 name as the class. That's why our first function has the same name as the class itself. And if we look closer still, we'll notice the whole purpose of the Box() function is to initialize the contents of the box. $this is used to tell Box() that contents is a varable that belongs to the whole class, not the function itself. $contents is a variable which only exists within the scope of the function Box(). $this->contents is a variable which was defined by 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" to it's initialization function. The initialization function, which has the same name as the class itself, accepts the value passed to it and stores it within the class's variable so that it's accessable to functions throughout the entire class. Now we've got a nice, brand new Jack in the Box (yes, I've been waiting the entire tutorial to say that). Methods To ask the box what it contains, the special get_whats_inside() function was used. The functions defined in the class are known as methods; they act as a method for communicating with and manipulating the data within the box. The nice thing about methods is that they allow us to separate all the class coding from our actual script. http://codewalkers.com/tutorials/54/1.html Page 3
  • 4. Beginning Object Oriented Programming in PHP by Timothy Boronczyk <?php include("class.Box.php"); $mybox = new Box("Jack"); echo $mybox->get_whats_inside(); ?> We could save all of the class code in a separate file (in this case I've named the file class.Box.php) 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. Another benefit of methods is that it provides our box, or whatever other objects we may build, with a standard interface that anyone can use. We can share our classes with other programmers or even import them into our other scripts where the functionality they provide is needed. <?php include("class.Box.php"); $mybox = new Box("Suggestion"); echo $mybox->get_whats_inside(); ?> <?php include("class.Box.php"); $mybox = new Box("Shoes"); echo $mybox->get_whats_inside(); ?> Extends A smart box is a wonderful object to have, but so far it's only capable of telling us what its content is. We don't have create an entirely new class to add new functionality... instead, we can build a small extention class based on the original. <?php include("class.Box.php"); class ShoeBox extends Box { http://codewalkers.com/tutorials/54/1.html Page 4
  • 5. Beginning Object Oriented Programming in PHP by Timothy Boronczyk var $size; function ShoeBox($contents, $size) { $this->contents = $contents; $this->size = $size; } function get_shoe_size() { return $this->size; } } $mybox = new ShoeBox("Shoes", 10); echo $mybox->get_whats_inside(); echo $mybox->get_shoe_size(); ?> With the extends keyword, our script now has a ShoeBox class based on our original Box class. ShoeBox has the same functionality as Box class, but with extra functions specific to a special kind of box. The ability to write such modular addons to your code gives great flexability in testing out new functions and saves time by reusing the same core code. <?php include("class.Box.php"); include("extentions.Shoe.php"); include("extentions.Suggestion.php"); include("extentions.Cardboard.php"); $mybox = new ShoeBox("Shoes", 10); $mySuggestion = new SuggestionBox("Complaints"); $myCardboard = new CardboardBox('', "corrugated", "18in", "12in", "10in"); ?> Conclusion You can see now how Object Oriented Programming got it's name--by focusing on building programs as a set of self-aware or smart objects. The ability to design modular code which OOP practices afford will help you save time, reduce stress and easily share your work with others. It isn't necessarily difficult, but rather it's the technical and philosophical jargon associated with it that can cause confusion for beginners. But, with perseverance and practice, your understanding of will grow... as so will your confidence! About the Author http://codewalkers.com/tutorials/54/1.html Page 5
  • 6. Beginning Object Oriented Programming in PHP by Timothy Boronczyk Timothy Boronczyk lives in Syracuse, NY, where he works as an E-Services Coordinator for a local credit union. He has a background in elementary education, over 5 years experience in web design and has written tutorials on web design, PHP, Ruby, XML and various other topics. His hobbies include photography and composing music. http://codewalkers.com/tutorials/54/1.html Page 6