 We have created our two separate 'person' objects, we can set their properties using the methods (the setters) we created.  class person {  var $name;

Slides:



Advertisements
Similar presentations
Introduction to Java 2 Programming
Advertisements

Fundamentals of Web DevelopmentRandy Connolly and Ricardo HoarFundamentals of Web DevelopmentRandy Connolly and Ricardo Hoar Fundamentals of Web DevelopmentRandy.
Inheritance Writing and using Classes effectively.
1 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Objectives Introduction to Inheritance and Composition (Subclasses and SuperClasses) Overriding (and extending), and inheriting methods and constructors.
PACS – 10/19/131 Object-Oriented Programming “Object-oriented programming opens the door to cleaner designs, easier maintenance, and greater code reuseability.”
More about classes and objects Classes in Visual Basic.NET.
CS-2135 Object Oriented Programming
Object-Oriented PHP (1)
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
1 Chapter 7 l Inheritance Basics l Programming with Inheritance l Dynamic Binding and Polymorphism Inheritance.
Terms and Rules Professor Evan Korth New York University (All rights reserved)
Applying OO Concepts Using Java. In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The.
PHP Workshop ‹#› PHP Classes and Object Orientation.
Object-Oriented Programming: Inheritance Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.
Object-Oriented Programming: Inheritance
Ruby (on Rails) Slides modified by ements-2ed.shtml) 1.
Inheritance using Java
UFCEUS-20-2 : Web Programming Lecture 5 : Object Oriented PHP (1)
Programming Languages and Paradigms Object-Oriented Programming.
Chapter 8 More Object Concepts
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
Tutorial 2 Variables and Objects. Working with Variables and Objects Variables (or identifiers) –Values stored in computer memory locations –Value can.
ClassesPHPMay-2007 : [‹#›] PHP Classes and Object Orientation.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
1 Classes and Controls CE-105 Spring 2007 By: Engr. Faisal ur Rehman.
CET203 SOFTWARE DEVELOPMENT Session 1A Revision of Classes.
Data Structures Using C++ 2E1 Inheritance An “is-a” relationship –Example: “every employee is a person” Allows new class creation from existing classes.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
2010/11 : [1]Building Web Applications using MySQL and PHP (W1)OO PHP PHP Classes and Object Orientation.
COMP Inheritance Basics Yi Hong June 09, 2015.
1 Inheritance Reserved word protected Reserved word super Overriding methods Class Hierarchies Reading for this lecture: L&L 9.1 – 9.4.
CITA 342 Section 1 Object Oriented Programming (OOP)
Classes in PHP Web Engineering. What is Class? A class is a collection of variables and functions working with these variables. Variables are defined.
Learning OOP in PHP. What is OOP? OOP stands for Object Oriented Programming. OOP is a programming paradigm wherein you create “objects” to work with.
1 / 71 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 4 Programming Fundamentals using Java 1.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
In this class, we will cover: Overriding a method Overloading a method Constructors Mutator and accessor methods The import statement and using prewritten.
Classes: Defining Your Own Data Types Basic principles in OOP Define a new data type as a class and use objects of a class Member Functions –Constructors.
5.1 Basics of defining and using classes A review of class and object definitions A class is a template or blueprint for an object A class defines.
 First thing we need to do is create two PHP pages:  index.php  class_lib.php  OOP is all about creating modular code, so our object oriented PHP.
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
COMPUTER SCIENCE & TECHNOLOGY DEGREE PROGRAMME FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UVA WELLASSA ‏ Properties of Object Oriented Programming.
Mr H Kandjimi 2016/01/03Mr Kandjimi1 Week 3 –Modularity in C++
 The real power of PHP comes from its functions; it has more than 1000 built-in functions.  PHP User Defined Functions  Besides the built-in PHP functions,
1 More About Derived Classes and Inheritance Chapter 9.
Object Oriented Programming. Constructors  Constructors are like special methods that are called implicitly as soon as an object is instantiated (i.e.
Object-oriented programming (OOP) is a programming paradigm using "objects" – data structures consisting of data fields and methods together with their.
CE-105 Spring 2007 By: Engr. Faisal ur Rehman
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Section 2.1: Programming paradigms
OOP: Encapsulation &Abstraction
Classes (Part 1) Lecture 3
Objects as a programming concept
2.7 Inheritance Types of inheritance
PHP Classes and Objects
Section 2.1: Programming paradigms
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
CS 106A, Lecture 22 More Classes
Object-Oriented Programming: Inheritance
Inheritance Basics Programming with Inheritance
CSCI 297 Scripting Languages Day Seven
Computer Programming with JAVA
Object-Oriented Programming in PHP
Inheritance Chapter 7 Inheritance Basics Programming with Inheritance
Fundaments of Game Design
Object-Oriented PHP (1)
Announcements Assignment 4 Due Today Lab 8 Due Wednesday
Object-Oriented Programming: Inheritance
CSG2H3 Object Oriented Programming
Presentation transcript:

 We have created our two separate 'person' objects, we can set their properties using the methods (the setters) we created.  class person {  var $name;  function set_name($new_name) {  $this->name = $new_name;  } }

  <?php  $stefan = new person();  $jimmy = new person();  $stefan->set_name("Stefan John");  $jimmy->set_name(“ Jimmy Nick");  ?> 

 <?php$stefan = new person();  $jimmy = new person();  $stefan->set_name("Stefan John");  $jimmy->set_name("Nick Jimmy");  echo "Stefan's full name: ". $stefan->get_name();  echo "Nick's full name: ". $jimmy->get_name();  ?> 

 You 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.  See Example on next slide

  <?php  $stefan = new person();  $jimmy = new person();  $stefan->set_name("Stefan John");  $jimmy->set_name("Nick Jimmy");  echo "Stefan's full name: ". $stefan->name;  ?> 

 All objects can have a special built-in method called a 'constructor'. Constructors allow you to initialize your object's properties.  The 'construct' method starts with two underscores (__) and the word 'construct'.  You 'feed' the constructor method by providing a list of arguments (like a function)  after the class name.

 <?php  2. class person {  3. var $name;  function __construct($persons_name) {  $this->name = $persons_name;  }  function set_name($new_name) {  $this->name = $new_name;  }

 Now that we've created a constructor method, we can provide a value for the $name property when we create our person objects.  For example: $stefan = new person("Stefan John");  <?php  $stefan = new person("Stefan Mischook");  echo "Stefan's full name: " $stefan->get_name();  ?>   Note: This saves us from having to call the set_name() method reducing the amount of code.

 One of the fundamental principles in OOP is 'encapsulation'. The idea is that you create cleaner better code, if you restrict access to the data structures (properties) in your objects.  You restrict access to class properties using something called 'access modifiers'.  There are 3 access modifiers:  public  private  protected  Public is the default modifier.

 <?php  class person {  var $name;  public $height;  protected $social_insurance;  private $pinn_number;  }  >  Note: When you declare a property with the 'var' keyword, it is considered 'public

 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 …more on that later.  Properties declared as 'public' have no access restrictions, meaning anyone can access them.

 Like properties, you can control access to methods using one of the three access  modifiers:  public  protected  private

 Doing this allows you to efficiently reuse the code found in your base class.  <?php  // 'extends' is the keyword that enables inheritance  class employee extends person {  function __construct($employee_name) { }  }  ?>

 Sometimes (when using inheritance,) you may need to change how a method works from the base class.  For example, let's say set_name() method in the 'employee' class, had to do something different than what it does in the 'person' class.

 Using :: allows you to specifically name the class where you want PHP to search for  a method - 'person::set_name()' tells PHP to search for set_name() in the 'person‘ class.  <?php  /* explicitly adding class properties are optional - but is good practice */ class person {  var $name;  function __construct($persons_name) {  $this->name = $persons_name;  }

 function get_name() {  return $this->name;  } // protected methods and properties restrict //access to those elements protected function set_name($new_name) {  if (name != "Jimmy Two Guns") {  $this->name = strtoupper($new_name); }  }

 protected function set_name($new_name) {  if ($new_name == "Stefan Lamp") {  $this->name = $new_name;  }  else if($new_name == "Johnny Fingers") {  parent::set_name($new_name);  }  function __construct($employee_name) {  $this->set_name($employee_name); }  }  ?>