SlideShare a Scribd company logo
1 of 6
Download to read offline
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)

Object oriented programming in php 5
Object oriented programming in php 5Object oriented programming in php 5
Object oriented programming in php 5
 
Oops in PHP
Oops in PHPOops in PHP
Oops in PHP
 
OOP in PHP
OOP in PHPOOP in PHP
OOP in PHP
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
 
Object Oriented PHP5
Object Oriented PHP5Object Oriented PHP5
Object Oriented PHP5
 
Introduction to PHP OOP
Introduction to PHP OOPIntroduction to PHP OOP
Introduction to PHP OOP
 
Basics of Object Oriented Programming in Python
Basics of Object Oriented Programming in PythonBasics of Object Oriented Programming in Python
Basics of Object Oriented Programming in Python
 
PHP Classes and OOPS Concept
PHP Classes and OOPS ConceptPHP Classes and OOPS Concept
PHP Classes and OOPS Concept
 
A Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented PhpA Gentle Introduction To Object Oriented Php
A Gentle Introduction To Object Oriented Php
 
Oops concept in php
Oops concept in phpOops concept in php
Oops concept in php
 
Intermediate OOP in PHP
Intermediate OOP in PHPIntermediate OOP in PHP
Intermediate OOP in PHP
 
Oops concepts in php
Oops concepts in phpOops concepts in php
Oops concepts in php
 
Dependency Injection Why is it awesome and Why should I care?
Dependency Injection Why is it awesome and Why should I care?Dependency Injection Why is it awesome and Why should I care?
Dependency Injection Why is it awesome and Why should I care?
 
Basic Oops concept of PHP
Basic Oops concept of PHPBasic Oops concept of PHP
Basic Oops concept of PHP
 
Introduction to OOP with PHP
Introduction to OOP with PHPIntroduction to OOP with PHP
Introduction to OOP with PHP
 
Introduction Php
Introduction PhpIntroduction Php
Introduction Php
 
Php oop presentation
Php   oop presentationPhp   oop presentation
Php oop presentation
 

Viewers also liked

Jeni Intro1 Bab01 Pengenalan Pemrograman Komputer
Jeni Intro1 Bab01 Pengenalan Pemrograman KomputerJeni Intro1 Bab01 Pengenalan Pemrograman Komputer
Jeni Intro1 Bab01 Pengenalan Pemrograman KomputerIndividual Consultants
 
php-mysql-tutorial-part-3
php-mysql-tutorial-part-3php-mysql-tutorial-part-3
php-mysql-tutorial-part-3tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />tutorialsruby
 
Real Time Link Building Strategies
Real Time Link Building StrategiesReal Time Link Building Strategies
Real Time Link Building Strategiesshuey03
 
Promoting growth through direct banking: anywhere, anytime, and device.
Promoting growth through direct banking: anywhere, anytime, and device.Promoting growth through direct banking: anywhere, anytime, and device.
Promoting growth through direct banking: anywhere, anytime, and device.Christopher Smith
 

Viewers also liked (7)

RicoAjaxEngine
RicoAjaxEngineRicoAjaxEngine
RicoAjaxEngine
 
The_Perl_Review_0_6
The_Perl_Review_0_6The_Perl_Review_0_6
The_Perl_Review_0_6
 
Jeni Intro1 Bab01 Pengenalan Pemrograman Komputer
Jeni Intro1 Bab01 Pengenalan Pemrograman KomputerJeni Intro1 Bab01 Pengenalan Pemrograman Komputer
Jeni Intro1 Bab01 Pengenalan Pemrograman Komputer
 
php-mysql-tutorial-part-3
php-mysql-tutorial-part-3php-mysql-tutorial-part-3
php-mysql-tutorial-part-3
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
Real Time Link Building Strategies
Real Time Link Building StrategiesReal Time Link Building Strategies
Real Time Link Building Strategies
 
Promoting growth through direct banking: anywhere, anytime, and device.
Promoting growth through direct banking: anywhere, anytime, and device.Promoting growth through direct banking: anywhere, anytime, and device.
Promoting growth through direct banking: anywhere, anytime, and device.
 

Similar to tutorial54

oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comtutorialsruby
 
Oop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.comOop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.comayandoesnotemail
 
Constructor and encapsulation in php
Constructor and encapsulation in phpConstructor and encapsulation in php
Constructor and encapsulation in phpSHIVANI SONI
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxShaownRoy1
 
Object And Oriented Programing ( Oop ) Languages
Object And Oriented Programing ( Oop ) LanguagesObject And Oriented Programing ( Oop ) Languages
Object And Oriented Programing ( Oop ) LanguagesJessica Deakin
 
From Java to Python: beating the Stockholm syndrome
From Java to Python: beating the Stockholm syndromeFrom Java to Python: beating the Stockholm syndrome
From Java to Python: beating the Stockholm syndromeJavier Arias Losada
 
Copy is a 4 letter word
Copy  is a 4 letter wordCopy  is a 4 letter word
Copy is a 4 letter wordMalcolm Murray
 
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScriptLotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScriptBill Buchan
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascriptUsman Mehmood
 
Embrace dynamic PHP
Embrace dynamic PHPEmbrace dynamic PHP
Embrace dynamic PHPPaul Houle
 
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...Bill Buchan
 
PHP OOP Lecture - 01.pptx
PHP OOP Lecture - 01.pptxPHP OOP Lecture - 01.pptx
PHP OOP Lecture - 01.pptxAtikur Rahman
 
Object-oriented programming 3.pptx
Object-oriented programming 3.pptxObject-oriented programming 3.pptx
Object-oriented programming 3.pptxAdikhan27
 

Similar to tutorial54 (20)

oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.com
 
Oop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.comOop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.com
 
Oop in php tutorial
Oop in php tutorialOop in php tutorial
Oop in php tutorial
 
Oop in php_tutorial
Oop in php_tutorialOop in php_tutorial
Oop in php_tutorial
 
Php oop (1)
Php oop (1)Php oop (1)
Php oop (1)
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Constructor and encapsulation in php
Constructor and encapsulation in phpConstructor and encapsulation in php
Constructor and encapsulation in php
 
Oop basic concepts
Oop basic conceptsOop basic concepts
Oop basic concepts
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
 
Object And Oriented Programing ( Oop ) Languages
Object And Oriented Programing ( Oop ) LanguagesObject And Oriented Programing ( Oop ) Languages
Object And Oriented Programing ( Oop ) Languages
 
From Java to Python: beating the Stockholm syndrome
From Java to Python: beating the Stockholm syndromeFrom Java to Python: beating the Stockholm syndrome
From Java to Python: beating the Stockholm syndrome
 
Copy is a 4 letter word
Copy  is a 4 letter wordCopy  is a 4 letter word
Copy is a 4 letter word
 
Ad507
Ad507Ad507
Ad507
 
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScriptLotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
Lotusphere 2007 BP301 Advanced Object Oriented Programming for LotusScript
 
Object oriented javascript
Object oriented javascriptObject oriented javascript
Object oriented javascript
 
Embrace dynamic PHP
Embrace dynamic PHPEmbrace dynamic PHP
Embrace dynamic PHP
 
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
Lotusphere 2007 AD507 Leveraging the Power of Object Oriented Programming in ...
 
PHP OOP Lecture - 01.pptx
PHP OOP Lecture - 01.pptxPHP OOP Lecture - 01.pptx
PHP OOP Lecture - 01.pptx
 
Object-oriented programming 3.pptx
Object-oriented programming 3.pptxObject-oriented programming 3.pptx
Object-oriented programming 3.pptx
 
Class 1
Class 1Class 1
Class 1
 

More from tutorialsruby

&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />tutorialsruby
 
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>tutorialsruby
 
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />tutorialsruby
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />tutorialsruby
 
Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269tutorialsruby
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269tutorialsruby
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008tutorialsruby
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008tutorialsruby
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheetstutorialsruby
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheetstutorialsruby
 

More from tutorialsruby (20)

&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
TopStyle Help &amp; &lt;b>Tutorial&lt;/b>
 
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
The Art Institute of Atlanta IMD 210 Fundamentals of Scripting &lt;b>...&lt;/b>
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />&lt;img src="../i/r_14.png" />
&lt;img src="../i/r_14.png" />
 
Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0Standardization and Knowledge Transfer – INS0
Standardization and Knowledge Transfer – INS0
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
 
xhtml_basics
xhtml_basicsxhtml_basics
xhtml_basics
 
xhtml-documentation
xhtml-documentationxhtml-documentation
xhtml-documentation
 
xhtml-documentation
xhtml-documentationxhtml-documentation
xhtml-documentation
 
CSS
CSSCSS
CSS
 
CSS
CSSCSS
CSS
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
 
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa0602690047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
0047ecaa6ea3e9ac0a13a2fe96f4de3bfd515c88f5d90c1fae79b956363d7f02c7fa060269
 
HowTo_CSS
HowTo_CSSHowTo_CSS
HowTo_CSS
 
HowTo_CSS
HowTo_CSSHowTo_CSS
HowTo_CSS
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
 
BloggingWithStyle_2008
BloggingWithStyle_2008BloggingWithStyle_2008
BloggingWithStyle_2008
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
 
cascadingstylesheets
cascadingstylesheetscascadingstylesheets
cascadingstylesheets
 

Recently uploaded

SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksSoftradix Technologies
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Neo4j
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Patryk Bandurski
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 

Recently uploaded (20)

Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Benefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other FrameworksBenefits Of Flutter Compared To Other Frameworks
Benefits Of Flutter Compared To Other Frameworks
 
Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024Build your next Gen AI Breakthrough - April 2024
Build your next Gen AI Breakthrough - April 2024
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
Integration and Automation in Practice: CI/CD in Mule Integration and Automat...
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 

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