SlideShare ist ein Scribd-Unternehmen logo
1 von 29
PHP Object Oriented Concepts
PHP 4, PHP 5 & PHP 6 ,[object Object],[object Object],[object Object],www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
Step by Step Process ,[object Object],[object Object],[object Object],www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
Object Oriented PHP How to develop a OO PHP ??   to get into this we are going to divide the process into 22 steps by which we can get a basic idea to develop an application in OOP Concepts. STEP 1 First lets create 2 PHP files index.php class_lib.php  OOP is all about creating modular code, so our object oriented PHP code will be contained in dedicated files that we will then insert into our normal PHP page using PHP 'includes'.  In this case, all our OO PHP code will be in the PHP file:  class_lib.php In OOP codes revolves around a 'class',  Classes are the templates that are used to define objects. STEP 2 Create a simple PHP class (in class_lib.php) Instead of having a bunch of functions, variables and code floating around, to design our PHP scripts in the OOP way, we need to create our own classes. keyword 'class'   www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 2  ( conti ...) <?php class   classname  { } ?> STEP 3  (add data to your class)  Classes are the blueprints for php objects. One of the big differences between functions and classes is that a class contains both data (variables) and functions that form a package called an: 'object'. When you create a variable inside a class, it is called a ' property '.  <?php class   classname  { // var $name is called as properties of class var keyword var  $name;  } ?> www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 4  (add functions/methods to your class)  Functions also referred by different name when created inside a class - they are called ' methods '.  A class's methods are used to manipulate its own data / properties.   <?php class  mfs_employee  { var  $name; function  set_name($new_name)  { $this -> name  =  $new_name; } function  get_name()  { return   $this -> name; } } ?> www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 5  (getter and setter functions)  We've created two interesting functions/methods:  get_name()  and  set_name() . These methods follow a common OOP convention that you see in many languages (including Java and Ruby) - where you create methods to  'set'  and  'get'  properties in a class. NOTE :  Another convention (a naming convention,) is that getter and setter names should match the property names.  This way, when other PHP programmers want to use your objects, they will know that if you have a method/function called 'set_name()', there will be a property/variable called 'name'.  <?php class  mfs_employee  { var  $name; function  set_name($new_name)  { $this -> name  =  $new_name; } function  get_name()  { return   $this -> name; } } ?> www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 6  (The '$this' variable)  $this -> name  =  $new_name; $this  is a built-in variable which points to the current object. Or in other words,  $this  is a special  self-referencing variable . We use  $this  to access properties and to call other methods of the current class. STEP 7  (Use our class in our main PHP page : index.php )  We should not create the PHP classes in our main page, else it will break the main purpose of building applications in OOP. So in index.php include the file (  class_lib.php  )  <?php   include (' class_lib.php' );  ?> www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 8  (  Instantiate/create your object  )  Classes are the blueprints/templates of php objects. Classes don't actually become objects until you do something called: instantiation. When you  instantiate  a class, you create an instance of it ... thus creating the  object . In other words, instantiation is the process of creating an instance of an object in memory.  What memory?  The server's memory of course! <?php $obj_mfsemp  =   new  mfs_employee(); ?> Note:   The variable  $obj_mfsemp  becomes a handle/reference to our newly created  mfs_employee   class. It is a 'handle', because we will use  $obj_mfsemp  to control and use the  mfs_employee   class. www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 9  ( new keyword  )  To create an  object  out of a class, you need to use the ' new ' keyword.  When creating/instantiating a class, we can  optionally  add brackets to the class name, as below example. To be clear, we can see in the code below how we create multiple objects from the same class. From the PHP's engine point of view, each  object  is its own  entity . <?php $obj_mfsemp1  =  new  mfs_employee (); $obj_mfsemp2  =  new  mfs_employee ; ?> www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 10  ( Set an objects properties  )  Now that we've created/instantiated our two separate ' mfs_employee ' objects, we can set their properties using the methods (the setters) we created. Please keep in mind that though both our  mfs_employee  objects ( $obj_mfsemp1  and  $obj_mfsemp2 ) are based on the same ' mfs_employee ' class, as far as php is concerned, they are totally different objects. <?php $obj_mfsemp1  =  new  mfs_employee (); $obj_mfsemp2  =  new  mfs_employee ; $obj_mfsemp1 -> set_name ( &quot;Abinash Grahacharya&quot; ); $obj_mfsemp2 -> set_name ( &quot;Amitabh Pattnaik&quot; ); ?> www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 11  ( Accessing an object's data  )  Now we use the getter methods to access the data held in our  objects  … this is the same data we inserted into our objects using the setter methods.  When accessing methods and properties of a class, we use the arrow  (->) operator.  <?php $obj_mfsemp1  =  new  mfs_employee (); $obj_mfsemp2  =  new  mfs_employee ; //setting values in the object $obj_mfsemp1 -> set_name ( &quot;Abinash Grahacharya&quot; ); $obj_mfsemp2 -> set_name ( &quot;Amitabh Pattnaik&quot; ); //getting each values from the object echo   $obj_mfsemp1  ->   get_name(); echo   &quot;<br />&quot; ; echo  $obj_mfsemp2   ->   get_name(); ?> www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
In this  short period of time, we have covered Designed a PHP class. Generate/created a couple of objects based on your class. Inserted data into your objects. Retrieved data from your objects. Lets now focus on PHP  OBJECT . www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 12  ( Directly accessing properties -  don't do it!  )  We don't have to use methods to access objects properties; you can directly get to them using the arrow operator (->) and the name of the variable. For example:  with the property  $name  (in object  $obj_mfsemp1 ,) we can get its' value like : <?php echo  $obj_mfsemp1 -> name ; ?> NOTE :  Though doable, it is considered bad practice to do it because it can lead to trouble down the road. We should use getter methods instead. www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 13  ( Constructor  )  All objects can have a special built-in method called  a ' constructor '. Constructors allow you to initialize your object's properties  (give values to properties)  when we instantiate (create) an  object .  Note:  If you create a  __construct()  function PHP will automatically call the  __construct()  method/function when you create an  object  from your class. The 'construct' method starts with two underscores  (__)  and the word ' construct '.  <?php class  mfs_employee  { var  $name; f unction  __construct($con_name)  { $this -> name  =  $con_name; } function  set_name($new_name)  { $this -> name  =  $new_name; } function  get_name()  { return   $this -> name; } } ?> www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 14  ( Create an object with a constructor  )  Now that we've created a constructor method, we can provide a value for the  $name  property when we create our  objects  for the class  mfs_employee .  We 'feed' the constructor method by providing a list of arguments (like we do with a function) after the class name at the time of object declaration. Not a constructor <?php $obj_mfsemp1  =  new  mfs_employee (); ?> When have constructor <?php $obj_con_mfsemp3  =  new  mfs_employee (“Abinash Grahacharya”); ?> www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 15  ( access modifiers  )  One of the fundamental principles in OOP is ' encapsulation '. The idea is that we create cleaner better code, if you restrict access to the data structures ( properties ) in our objects.  Encapsulation :  Storing  data/properties  and  functions/methods  in a single unit (class) is encapsulation. Data cannot be accessible to the outside world and only those functions which are stored in the class can access it. We restrict access to class properties using something called ' access modifiers '. There are 3  access modifiers : 1. public 2. private 3. protected ' Public ' is the default modifier. www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 15  ( access modifiers  ) conti... <?php class  mfs_employee  { var  $name; public  $designation =  'SW Engineer' ; protected  $standard_charted_pin =  '756472' ; private  $gps_password =  'mindfire' ; f unction  __construct($con_name)  { $this -> name  =  $con_name; } function  set_name($new_name)  { $this -> name  =  $new_name; } function  get_name()  { return   $this -> name; } } / /NOTE : when ever we are using var it is treated as public ?> www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 16  ( Restricting access to properties  )  Properties declared as ' public ' have no access restrictions, meaning anyone can access them. When you declare a property as ' private ', only the same class can access the property. When a property is declared ' protected ', only the same class and classes derived from that class can access the property - this has to do with inheritance  <?php $obj_mfsemp1  =  new  mfs_employee (“Mindfire”); echo  $obj_mfsemp1 ->  get_name(); //when we try to access private or public properties outside class will through Fatal Error echo  $obj_mfsemp1 ->  standard_charted_pin; ?> www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 17  ( Restricting access to methods  )  Like properties, you can control access to methods using one of the three access modifiers: 1. public 2. protected 3. private <?php class  mfs_employee  { var  $name; public  $designation =  'SW Engineer' ; protected  $standard_charted_pin =  '756472' ; private  $gps_password =  'mindfire' ; private function  getpin()  { return   $this -> standard_charted_pin   ; } } ?> Since the method  getpin()  is ' private ', the only place you can use this method is in the same class - typically in another method in class. If we wanted to call/use this method directly in our PHP pages, we  need to declare it as ' public '. www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 18  ( Inheritance - reusing code the OOP way  )  Inheritance is a fundamental capability/construct in OOP where you can use one class, as the base/basis for another class … or many other classes. Why do it? Doing this allows help to efficiently reuse the code found in our base class. Say, you wanted to create a new ' sales_people ' class … since we can say that ' mfs_employee ' is a type/kind of 'peoples', they will share common properties and methods. In this type of situation, inheritance can make our code lighter … because we are reusing the same code in two different classes. 1. You only have to type the code out once. 2. The actual code being reused, can be reused in many classes but it is only typed out in one place … conceptually, this is sort-of like PHP includes(). www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 18  ( Inheritance - reusing code the OOP way  )  conti.. // 'extends' is the keyword that enables inheritance class  sales_people extends mfs_employee { function  __construct($employee_name)  { $this  ->  set_name($employee_name); } } <?php class  mfs_employee  { var  $name; public  $designation =  'SW Engineer' ; protected  $standard_charted_pin =  '756472' ; private  $gps_password =  'mindfire' ; f unction  __construct($con_name)  { $this -> name  =  $con_name; } function  set_name($new_name)  { $this -> name  =  $new_name; } function  get_name()  { return   $this -> name; } } / /NOTE : when ever we are using var it is treated as public ?> www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 19  ( Inheritance - reusing code the OOP way how to access  )  Because the class ' sales_people ' is based on the class ' mfs_employee ', ' sales_people ' automatically has all the  public  and  protected,   properties  and  methods  of ' mfs_employee ' class. Notice how we are able to use  set_name()  in ' sales_people ', even though we did not declare that method in the ' sales_people ' class. That's because we already created  set_name()  in the class ' mfs_employee '. Note:  the ' sales_people ' class is called children the 'base' class or the ' mfs_employee ' class because it's the class that the ' sales_people ' is based on. This class hierarchy can become important down the road when our projects become more complex.  // 'extends' is the keyword that enables inheritance class  sales_people extends mfs_employee { function  __construct($employee_name)  { $this  ->  set_name($employee_name); } } www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 20  ( Inheritance - reusing code the OOP way- How to access  )  <?php class  mfs_employee  { var  $name; public  $designation =  'SW Engineer' ; protected  $standard_charted_pin =  '756472' ; private  $gps_password =  'mindfire' ; f unction  __construct($con_name)  { $this -> name  =  $con_name; } function  set_name($new_name)  { $this -> name  =  $new_name; } function  get_name()  { return   $this -> name; } } / /NOTE : when ever we are using var it is treated as public ?> // 'extends' is the keyword that enables inheritance class  sales_people extends mfs_employee { function  __construct($employee_name)  { $this  ->  set_name($employee_name); } } //In PHP file <?php $sp_obj_c2  =   new  sales_people( &quot;class2 names&quot; );  echo  $sp_obj_c2  ->  get_name() ; ?> This is a classic example of how OOP can reduce the number of lines of code (don't have to write the same methods twice) while still keeping your code modular and much easier to maintain.  www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 21  ( Overriding  Methods  )  Sometimes (when using inheritance,) we may need to change how a method works from the base class. For example, let's say  set_name()  method in the ' sales_people ' class, have to do something different than what it does in the ' mfs_employee ' class.  We have to  ' override ' the '' mfs_employee ' classes version of  set_name() , by declaring the same method in ' sales_people '.   // 'extends' is the keyword that enables inheritance class  sales_people extends mfs_employee { function  __construct($employee_name)  { $this  ->  set_name($employee_name); } function  set_name($new_name)  { if ($new_name[0] == &quot;S&quot;)  { $this->name = $new_name; } } } //In PHP file <?php $sp_obj_c2  =   new  sales_people( &quot;class2 names&quot; );  echo  $sp_obj_c2  ->  get_name() ; $sp_obj_c2  =   new  sales_people( &quot;So Check it&quot; );  echo  $sp_obj_c2  ->  get_name() ; ?> www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 22  ( Overriding  Methods  ) cont..  Sometimes we may need to access our base class's version of a method  over lode in the derived (sometimes called 'child') class. In our example, we overrode the  set_name()  method in the ' sales_people ' class. Now We have to used the following code :   mfs_employee :: set_name($new_name); to access the parent class' ( mfs_employee ) version of the  set_name()  method // 'extends' is the keyword that enables inheritance class  sales_people extends mfs_employee { function  __construct($employee_name)  { $this  ->  set_name($employee_name); } function  set_name($new_name)  { if ($new_name == &quot;Stefan Sucks&quot;)  { $this->name = $new_name; } } function   set_name_old_style($new_name)  {   mfs_employee :: set_name($new_name); } } ::  will tell to PHP  to search for  set_name()  in the 'base' class.  www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
STEP 22  ( Overriding  Methods  ) cont..  Also by using the  parent  keyword we can call the parent methods if it is overloaded // 'extends' is the keyword that enables inheritance class  sales_people extends mfs_employee { function  __construct($employee_name)  { $this  ->  set_name($employee_name); } function  set_name($new_name)  { if ($new_name == &quot;Stefan Sucks&quot;)  { $this->name = $new_name; } } function   set_name_old_style($new_name)  {   parent :: set_name($new_name); } } www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
Our Expertise in PHP ,[object Object],[object Object],[object Object],www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions
Thank you for viewing the slides. Hope it did add value. www.mindfiresolutions.com   |  www.twitter.com/mindfires   |  http:// wikipedia.org/wiki/mindfire_solutions   For further queries  contact us   or call 1-248-686-1424 www.mindfiresolutions.com

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Loops PHP 04
Loops PHP 04Loops PHP 04
Loops PHP 04
 
Php operators
Php operatorsPhp operators
Php operators
 
Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1Web Development Course: PHP lecture 1
Web Development Course: PHP lecture 1
 
Php and MySQL
Php and MySQLPhp and MySQL
Php and MySQL
 
Introduction to ADO.NET
Introduction to ADO.NETIntroduction to ADO.NET
Introduction to ADO.NET
 
Introduction to php
Introduction to phpIntroduction to php
Introduction to php
 
Php Unit 1
Php Unit 1Php Unit 1
Php Unit 1
 
Introduction To PHP
Introduction To PHPIntroduction To PHP
Introduction To PHP
 
Php technical presentation
Php technical presentationPhp technical presentation
Php technical presentation
 
JavaScript - An Introduction
JavaScript - An IntroductionJavaScript - An Introduction
JavaScript - An Introduction
 
5. stored procedure and functions
5. stored procedure and functions5. stored procedure and functions
5. stored procedure and functions
 
Php introduction
Php introductionPhp introduction
Php introduction
 
Php Presentation
Php PresentationPhp Presentation
Php Presentation
 
Php.ppt
Php.pptPhp.ppt
Php.ppt
 
PHP slides
PHP slidesPHP slides
PHP slides
 
PHP Loops and PHP Forms
PHP  Loops and PHP FormsPHP  Loops and PHP Forms
PHP Loops and PHP Forms
 
Java Servlets
Java ServletsJava Servlets
Java Servlets
 
Uploading a file with php
Uploading a file with phpUploading a file with php
Uploading a file with php
 
Database Connectivity in PHP
Database Connectivity in PHPDatabase Connectivity in PHP
Database Connectivity in PHP
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 

Andere mochten auch

Beginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPBeginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPRick Ogden
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisIan Macali
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHPBradley Holt
 
Login and Registration form using oop in php
Login and Registration form using oop in phpLogin and Registration form using oop in php
Login and Registration form using oop in phpherat university
 
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 PhpMichael Girouard
 
Object Oriented Design Patterns for PHP
Object Oriented Design Patterns for PHPObject Oriented Design Patterns for PHP
Object Oriented Design Patterns for PHPRobertGonzalez
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP FunctionsAhmed Swilam
 
HTML5 for PHP Developers - IPC
HTML5 for PHP Developers - IPCHTML5 for PHP Developers - IPC
HTML5 for PHP Developers - IPCMayflower GmbH
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingAhmed Swilam
 
Open Source Package PHP & MySQL
Open Source Package PHP & MySQLOpen Source Package PHP & MySQL
Open Source Package PHP & MySQLkalaisai
 
Top 100 PHP Questions and Answers
Top 100 PHP Questions and AnswersTop 100 PHP Questions and Answers
Top 100 PHP Questions and Answersiimjobs and hirist
 
Class 6 - PHP Web Programming
Class 6 - PHP Web ProgrammingClass 6 - PHP Web Programming
Class 6 - PHP Web ProgrammingAhmed Swilam
 
Alphorm.com Support de la Formation PHP MySQL
Alphorm.com Support de la Formation PHP MySQLAlphorm.com Support de la Formation PHP MySQL
Alphorm.com Support de la Formation PHP MySQLAlphorm
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Conceptsthinkphp
 

Andere mochten auch (20)

Beginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHPBeginners Guide to Object Orientation in PHP
Beginners Guide to Object Orientation in PHP
 
PHP Classes and OOPS Concept
PHP Classes and OOPS ConceptPHP Classes and OOPS Concept
PHP Classes and OOPS Concept
 
PHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with thisPHP Powerpoint -- Teach PHP with this
PHP Powerpoint -- Teach PHP with this
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Login and Registration form using oop in php
Login and Registration form using oop in phpLogin and Registration form using oop in php
Login and Registration form using oop in php
 
OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)OOPS Characteristics (With Examples in PHP)
OOPS Characteristics (With Examples in PHP)
 
Arrays in PHP
Arrays in PHPArrays in PHP
Arrays in PHP
 
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
 
Inheritance
InheritanceInheritance
Inheritance
 
Object Oriented Design Patterns for PHP
Object Oriented Design Patterns for PHPObject Oriented Design Patterns for PHP
Object Oriented Design Patterns for PHP
 
Class 3 - PHP Functions
Class 3 - PHP FunctionsClass 3 - PHP Functions
Class 3 - PHP Functions
 
HTML5 for PHP Developers - IPC
HTML5 for PHP Developers - IPCHTML5 for PHP Developers - IPC
HTML5 for PHP Developers - IPC
 
Class 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented ProgrammingClass 7 - PHP Object Oriented Programming
Class 7 - PHP Object Oriented Programming
 
PHP Security
PHP SecurityPHP Security
PHP Security
 
Open Source Package PHP & MySQL
Open Source Package PHP & MySQLOpen Source Package PHP & MySQL
Open Source Package PHP & MySQL
 
Top 100 PHP Questions and Answers
Top 100 PHP Questions and AnswersTop 100 PHP Questions and Answers
Top 100 PHP Questions and Answers
 
Class 6 - PHP Web Programming
Class 6 - PHP Web ProgrammingClass 6 - PHP Web Programming
Class 6 - PHP Web Programming
 
Alphorm.com Support de la Formation PHP MySQL
Alphorm.com Support de la Formation PHP MySQLAlphorm.com Support de la Formation PHP MySQL
Alphorm.com Support de la Formation PHP MySQL
 
Object Oriented Programming Concepts
Object Oriented Programming ConceptsObject Oriented Programming Concepts
Object Oriented Programming Concepts
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 

Ähnlich wie Oops in PHP

Basic Oops concept of PHP
Basic Oops concept of PHPBasic Oops concept of PHP
Basic Oops concept of PHPRohan Sharma
 
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.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
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPsRavi Bhadauria
 
Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Jalpesh Vasa
 
Oops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperOops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperNyros Technologies
 
Embrace dynamic PHP
Embrace dynamic PHPEmbrace dynamic PHP
Embrace dynamic PHPPaul Houle
 
Php interview questions
Php interview questionsPhp interview questions
Php interview questionssekar c
 
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
 
Dependency Injection for PHP
Dependency Injection for PHPDependency Injection for PHP
Dependency Injection for PHPmtoppa
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindiappsdevelopment
 
Intro to OOP PHP and Github
Intro to OOP PHP and GithubIntro to OOP PHP and Github
Intro to OOP PHP and GithubJo Erik San Jose
 

Ähnlich wie Oops in PHP (20)

Oop's in php
Oop's in php Oop's in php
Oop's in php
 
Oops in php
Oops in phpOops in php
Oops in php
 
Basic Oops concept of PHP
Basic Oops concept of PHPBasic Oops concept of PHP
Basic Oops concept of PHP
 
Introduction Php
Introduction PhpIntroduction Php
Introduction Php
 
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_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
 
Object oreinted php | OOPs
Object oreinted php | OOPsObject oreinted php | OOPs
Object oreinted php | OOPs
 
Oops concept in php
Oops concept in phpOops concept in php
Oops concept in php
 
Object Oriented PHP - PART-1
Object Oriented PHP - PART-1Object Oriented PHP - PART-1
Object Oriented PHP - PART-1
 
Oops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperOops in PHP By Nyros Developer
Oops in PHP By Nyros Developer
 
Embrace dynamic PHP
Embrace dynamic PHPEmbrace dynamic PHP
Embrace dynamic PHP
 
Php interview questions
Php interview questionsPhp interview questions
Php interview questions
 
Constructor and encapsulation in php
Constructor and encapsulation in phpConstructor and encapsulation in php
Constructor and encapsulation in php
 
Lecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptxLecture-10_PHP-OOP.pptx
Lecture-10_PHP-OOP.pptx
 
Dependency Injection for PHP
Dependency Injection for PHPDependency Injection for PHP
Dependency Injection for PHP
 
Synapseindia object oriented programming in php
Synapseindia object oriented programming in phpSynapseindia object oriented programming in php
Synapseindia object oriented programming in php
 
Intro to OOP PHP and Github
Intro to OOP PHP and GithubIntro to OOP PHP and Github
Intro to OOP PHP and Github
 

Mehr von Mindfire Solutions (20)

Physician Search and Review
Physician Search and ReviewPhysician Search and Review
Physician Search and Review
 
diet management app
diet management appdiet management app
diet management app
 
Business Technology Solution
Business Technology SolutionBusiness Technology Solution
Business Technology Solution
 
Remote Health Monitoring
Remote Health MonitoringRemote Health Monitoring
Remote Health Monitoring
 
Influencer Marketing Solution
Influencer Marketing SolutionInfluencer Marketing Solution
Influencer Marketing Solution
 
ELMAH
ELMAHELMAH
ELMAH
 
High Availability of Azure Applications
High Availability of Azure ApplicationsHigh Availability of Azure Applications
High Availability of Azure Applications
 
IOT Hands On
IOT Hands OnIOT Hands On
IOT Hands On
 
Glimpse of Loops Vs Set
Glimpse of Loops Vs SetGlimpse of Loops Vs Set
Glimpse of Loops Vs Set
 
Oracle Sql Developer-Getting Started
Oracle Sql Developer-Getting StartedOracle Sql Developer-Getting Started
Oracle Sql Developer-Getting Started
 
Adaptive Layout In iOS 8
Adaptive Layout In iOS 8Adaptive Layout In iOS 8
Adaptive Layout In iOS 8
 
Introduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/MacIntroduction to Auto-layout : iOS/Mac
Introduction to Auto-layout : iOS/Mac
 
LINQPad - utility Tool
LINQPad - utility ToolLINQPad - utility Tool
LINQPad - utility Tool
 
Get started with watch kit development
Get started with watch kit developmentGet started with watch kit development
Get started with watch kit development
 
Swift vs Objective-C
Swift vs Objective-CSwift vs Objective-C
Swift vs Objective-C
 
Material Design in Android
Material Design in AndroidMaterial Design in Android
Material Design in Android
 
Introduction to OData
Introduction to ODataIntroduction to OData
Introduction to OData
 
Ext js Part 2- MVC
Ext js Part 2- MVCExt js Part 2- MVC
Ext js Part 2- MVC
 
ExtJs Basic Part-1
ExtJs Basic Part-1ExtJs Basic Part-1
ExtJs Basic Part-1
 
Spring Security Introduction
Spring Security IntroductionSpring Security Introduction
Spring Security Introduction
 

Kürzlich hochgeladen

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 

Kürzlich hochgeladen (20)

From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

Oops in PHP

  • 2.
  • 3.
  • 4. Object Oriented PHP How to develop a OO PHP ?? to get into this we are going to divide the process into 22 steps by which we can get a basic idea to develop an application in OOP Concepts. STEP 1 First lets create 2 PHP files index.php class_lib.php OOP is all about creating modular code, so our object oriented PHP code will be contained in dedicated files that we will then insert into our normal PHP page using PHP 'includes'. In this case, all our OO PHP code will be in the PHP file: class_lib.php In OOP codes revolves around a 'class', Classes are the templates that are used to define objects. STEP 2 Create a simple PHP class (in class_lib.php) Instead of having a bunch of functions, variables and code floating around, to design our PHP scripts in the OOP way, we need to create our own classes. keyword 'class' www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 5. STEP 2 ( conti ...) <?php class classname { } ?> STEP 3 (add data to your class) Classes are the blueprints for php objects. One of the big differences between functions and classes is that a class contains both data (variables) and functions that form a package called an: 'object'. When you create a variable inside a class, it is called a ' property '. <?php class classname { // var $name is called as properties of class var keyword var $name; } ?> www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 6. STEP 4 (add functions/methods to your class) Functions also referred by different name when created inside a class - they are called ' methods '. A class's methods are used to manipulate its own data / properties. <?php class mfs_employee { var $name; function set_name($new_name) { $this -> name = $new_name; } function get_name() { return $this -> name; } } ?> www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 7. STEP 5 (getter and setter functions) We've created two interesting functions/methods: get_name() and set_name() . These methods follow a common OOP convention that you see in many languages (including Java and Ruby) - where you create methods to 'set' and 'get' properties in a class. NOTE : Another convention (a naming convention,) is that getter and setter names should match the property names. This way, when other PHP programmers want to use your objects, they will know that if you have a method/function called 'set_name()', there will be a property/variable called 'name'. <?php class mfs_employee { var $name; function set_name($new_name) { $this -> name = $new_name; } function get_name() { return $this -> name; } } ?> www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 8. STEP 6 (The '$this' variable) $this -> name = $new_name; $this is a built-in variable which points to the current object. Or in other words, $this is a special self-referencing variable . We use $this to access properties and to call other methods of the current class. STEP 7 (Use our class in our main PHP page : index.php ) We should not create the PHP classes in our main page, else it will break the main purpose of building applications in OOP. So in index.php include the file ( class_lib.php ) <?php include (' class_lib.php' ); ?> www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 9. STEP 8 ( Instantiate/create your object ) Classes are the blueprints/templates of php objects. Classes don't actually become objects until you do something called: instantiation. When you instantiate a class, you create an instance of it ... thus creating the object . In other words, instantiation is the process of creating an instance of an object in memory. What memory? The server's memory of course! <?php $obj_mfsemp = new mfs_employee(); ?> Note: The variable $obj_mfsemp becomes a handle/reference to our newly created mfs_employee class. It is a 'handle', because we will use $obj_mfsemp to control and use the mfs_employee class. www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 10. STEP 9 ( new keyword ) To create an object out of a class, you need to use the ' new ' keyword. When creating/instantiating a class, we can optionally add brackets to the class name, as below example. To be clear, we can see in the code below how we create multiple objects from the same class. From the PHP's engine point of view, each object is its own entity . <?php $obj_mfsemp1 = new mfs_employee (); $obj_mfsemp2 = new mfs_employee ; ?> www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 11. STEP 10 ( Set an objects properties ) Now that we've created/instantiated our two separate ' mfs_employee ' objects, we can set their properties using the methods (the setters) we created. Please keep in mind that though both our mfs_employee objects ( $obj_mfsemp1 and $obj_mfsemp2 ) are based on the same ' mfs_employee ' class, as far as php is concerned, they are totally different objects. <?php $obj_mfsemp1 = new mfs_employee (); $obj_mfsemp2 = new mfs_employee ; $obj_mfsemp1 -> set_name ( &quot;Abinash Grahacharya&quot; ); $obj_mfsemp2 -> set_name ( &quot;Amitabh Pattnaik&quot; ); ?> www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 12. STEP 11 ( Accessing an object's data ) Now we use the getter methods to access the data held in our objects … this is the same data we inserted into our objects using the setter methods. When accessing methods and properties of a class, we use the arrow (->) operator. <?php $obj_mfsemp1 = new mfs_employee (); $obj_mfsemp2 = new mfs_employee ; //setting values in the object $obj_mfsemp1 -> set_name ( &quot;Abinash Grahacharya&quot; ); $obj_mfsemp2 -> set_name ( &quot;Amitabh Pattnaik&quot; ); //getting each values from the object echo $obj_mfsemp1 -> get_name(); echo &quot;<br />&quot; ; echo $obj_mfsemp2 -> get_name(); ?> www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 13. In this short period of time, we have covered Designed a PHP class. Generate/created a couple of objects based on your class. Inserted data into your objects. Retrieved data from your objects. Lets now focus on PHP OBJECT . www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 14. STEP 12 ( Directly accessing properties - don't do it! ) We don't have to use methods to access objects properties; you can directly get to them using the arrow operator (->) and the name of the variable. For example: with the property $name (in object $obj_mfsemp1 ,) we can get its' value like : <?php echo $obj_mfsemp1 -> name ; ?> NOTE : Though doable, it is considered bad practice to do it because it can lead to trouble down the road. We should use getter methods instead. www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 15. STEP 13 ( Constructor ) All objects can have a special built-in method called a ' constructor '. Constructors allow you to initialize your object's properties (give values to properties) when we instantiate (create) an object . Note: If you create a __construct() function PHP will automatically call the __construct() method/function when you create an object from your class. The 'construct' method starts with two underscores (__) and the word ' construct '. <?php class mfs_employee { var $name; f unction __construct($con_name) { $this -> name = $con_name; } function set_name($new_name) { $this -> name = $new_name; } function get_name() { return $this -> name; } } ?> www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 16. STEP 14 ( Create an object with a constructor ) Now that we've created a constructor method, we can provide a value for the $name property when we create our objects for the class mfs_employee . We 'feed' the constructor method by providing a list of arguments (like we do with a function) after the class name at the time of object declaration. Not a constructor <?php $obj_mfsemp1 = new mfs_employee (); ?> When have constructor <?php $obj_con_mfsemp3 = new mfs_employee (“Abinash Grahacharya”); ?> www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 17. STEP 15 ( access modifiers ) One of the fundamental principles in OOP is ' encapsulation '. The idea is that we create cleaner better code, if you restrict access to the data structures ( properties ) in our objects. Encapsulation : Storing data/properties and functions/methods in a single unit (class) is encapsulation. Data cannot be accessible to the outside world and only those functions which are stored in the class can access it. We restrict access to class properties using something called ' access modifiers '. There are 3 access modifiers : 1. public 2. private 3. protected ' Public ' is the default modifier. www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 18. STEP 15 ( access modifiers ) conti... <?php class mfs_employee { var $name; public $designation = 'SW Engineer' ; protected $standard_charted_pin = '756472' ; private $gps_password = 'mindfire' ; f unction __construct($con_name) { $this -> name = $con_name; } function set_name($new_name) { $this -> name = $new_name; } function get_name() { return $this -> name; } } / /NOTE : when ever we are using var it is treated as public ?> www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 19. STEP 16 ( Restricting access to properties ) Properties declared as ' public ' have no access restrictions, meaning anyone can access them. When you declare a property as ' private ', only the same class can access the property. When a property is declared ' protected ', only the same class and classes derived from that class can access the property - this has to do with inheritance <?php $obj_mfsemp1 = new mfs_employee (“Mindfire”); echo $obj_mfsemp1 -> get_name(); //when we try to access private or public properties outside class will through Fatal Error echo $obj_mfsemp1 -> standard_charted_pin; ?> www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 20. STEP 17 ( Restricting access to methods ) Like properties, you can control access to methods using one of the three access modifiers: 1. public 2. protected 3. private <?php class mfs_employee { var $name; public $designation = 'SW Engineer' ; protected $standard_charted_pin = '756472' ; private $gps_password = 'mindfire' ; private function getpin() { return $this -> standard_charted_pin ; } } ?> Since the method getpin() is ' private ', the only place you can use this method is in the same class - typically in another method in class. If we wanted to call/use this method directly in our PHP pages, we need to declare it as ' public '. www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 21. STEP 18 ( Inheritance - reusing code the OOP way ) Inheritance is a fundamental capability/construct in OOP where you can use one class, as the base/basis for another class … or many other classes. Why do it? Doing this allows help to efficiently reuse the code found in our base class. Say, you wanted to create a new ' sales_people ' class … since we can say that ' mfs_employee ' is a type/kind of 'peoples', they will share common properties and methods. In this type of situation, inheritance can make our code lighter … because we are reusing the same code in two different classes. 1. You only have to type the code out once. 2. The actual code being reused, can be reused in many classes but it is only typed out in one place … conceptually, this is sort-of like PHP includes(). www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 22. STEP 18 ( Inheritance - reusing code the OOP way ) conti.. // 'extends' is the keyword that enables inheritance class sales_people extends mfs_employee { function __construct($employee_name) { $this -> set_name($employee_name); } } <?php class mfs_employee { var $name; public $designation = 'SW Engineer' ; protected $standard_charted_pin = '756472' ; private $gps_password = 'mindfire' ; f unction __construct($con_name) { $this -> name = $con_name; } function set_name($new_name) { $this -> name = $new_name; } function get_name() { return $this -> name; } } / /NOTE : when ever we are using var it is treated as public ?> www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 23. STEP 19 ( Inheritance - reusing code the OOP way how to access ) Because the class ' sales_people ' is based on the class ' mfs_employee ', ' sales_people ' automatically has all the public and protected, properties and methods of ' mfs_employee ' class. Notice how we are able to use set_name() in ' sales_people ', even though we did not declare that method in the ' sales_people ' class. That's because we already created set_name() in the class ' mfs_employee '. Note: the ' sales_people ' class is called children the 'base' class or the ' mfs_employee ' class because it's the class that the ' sales_people ' is based on. This class hierarchy can become important down the road when our projects become more complex. // 'extends' is the keyword that enables inheritance class sales_people extends mfs_employee { function __construct($employee_name) { $this -> set_name($employee_name); } } www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 24. STEP 20 ( Inheritance - reusing code the OOP way- How to access ) <?php class mfs_employee { var $name; public $designation = 'SW Engineer' ; protected $standard_charted_pin = '756472' ; private $gps_password = 'mindfire' ; f unction __construct($con_name) { $this -> name = $con_name; } function set_name($new_name) { $this -> name = $new_name; } function get_name() { return $this -> name; } } / /NOTE : when ever we are using var it is treated as public ?> // 'extends' is the keyword that enables inheritance class sales_people extends mfs_employee { function __construct($employee_name) { $this -> set_name($employee_name); } } //In PHP file <?php $sp_obj_c2 = new sales_people( &quot;class2 names&quot; ); echo $sp_obj_c2 -> get_name() ; ?> This is a classic example of how OOP can reduce the number of lines of code (don't have to write the same methods twice) while still keeping your code modular and much easier to maintain. www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 25. STEP 21 ( Overriding Methods ) Sometimes (when using inheritance,) we may need to change how a method works from the base class. For example, let's say set_name() method in the ' sales_people ' class, have to do something different than what it does in the ' mfs_employee ' class. We have to ' override ' the '' mfs_employee ' classes version of set_name() , by declaring the same method in ' sales_people '. // 'extends' is the keyword that enables inheritance class sales_people extends mfs_employee { function __construct($employee_name) { $this -> set_name($employee_name); } function set_name($new_name) { if ($new_name[0] == &quot;S&quot;) { $this->name = $new_name; } } } //In PHP file <?php $sp_obj_c2 = new sales_people( &quot;class2 names&quot; ); echo $sp_obj_c2 -> get_name() ; $sp_obj_c2 = new sales_people( &quot;So Check it&quot; ); echo $sp_obj_c2 -> get_name() ; ?> www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 26. STEP 22 ( Overriding Methods ) cont.. Sometimes we may need to access our base class's version of a method over lode in the derived (sometimes called 'child') class. In our example, we overrode the set_name() method in the ' sales_people ' class. Now We have to used the following code : mfs_employee :: set_name($new_name); to access the parent class' ( mfs_employee ) version of the set_name() method // 'extends' is the keyword that enables inheritance class sales_people extends mfs_employee { function __construct($employee_name) { $this -> set_name($employee_name); } function set_name($new_name) { if ($new_name == &quot;Stefan Sucks&quot;) { $this->name = $new_name; } } function set_name_old_style($new_name) { mfs_employee :: set_name($new_name); } } :: will tell to PHP to search for set_name() in the 'base' class. www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 27. STEP 22 ( Overriding Methods ) cont.. Also by using the parent keyword we can call the parent methods if it is overloaded // 'extends' is the keyword that enables inheritance class sales_people extends mfs_employee { function __construct($employee_name) { $this -> set_name($employee_name); } function set_name($new_name) { if ($new_name == &quot;Stefan Sucks&quot;) { $this->name = $new_name; } } function set_name_old_style($new_name) { parent :: set_name($new_name); } } www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions
  • 28.
  • 29. Thank you for viewing the slides. Hope it did add value. www.mindfiresolutions.com | www.twitter.com/mindfires | http:// wikipedia.org/wiki/mindfire_solutions For further queries contact us or call 1-248-686-1424 www.mindfiresolutions.com