1 / 15

Object-Oriented Programming

Object-Oriented Programming. “Object-oriented programming opens the door to cleaner designs, easier maintenance, and greater code reuseability.” – Opening of Chapter 6, “Programming PHP” from O’Reilly.

clark
Download Presentation

Object-Oriented Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Programming • “Object-oriented programming opens the door to cleaner designs, easier maintenance, and greater code reuseability.” – Opening of Chapter 6, “Programming PHP” from O’Reilly. • A different way of coding. Your code won’t run faster, be more accurate, have prettier output, etc. • Although the concept dates from the 1950’s, Simula from 1967 is probably the first language. PACS – 10/19/13

  2. Object-Oriented Programming • Smalltalk from 1972 was the first OOP language in widespread use. • In the mid 1990’s, OOP formed the basis for C++, Java, C#, VB.NET, Python and others. • Other languages have had OOP extensions added: Visual Basic, Fortran, Perl, Cobol, etc. • And PHP starting in v4. OOP support was much improved in v5. PACS – 10/19/13

  3. Object-Oriented Programming • Non-OOP is called procedural or modular coding. • First code was one big glob. • Next came separation of code into functional units using subroutines or functions. • OOP further separates the data and code into ‘objects’. This makes it easier to debug because only the associated code works with the data. • Objects won’t touch another’s data. PACS – 10/19/13

  4. Object-Oriented Programming • As with anything else new, OOP comes with its own terminology and we need to learn some of these terms before we can write our own code or can use someone else’s code library. • A class is a template for building objects. • An object is an instance or occurrence of a class. • An object will have user data with attached code to work with that data. PACS – 10/19/13

  5. Object-Oriented Programming • The data associated with an object are called its properties. • The functions associated with an object are called its methods. • When you define a class, you define the names of its properties and give the code for its methods. • Encapsulation is the idea that a class’s methods deals with the class properties and no other method does. PACS – 10/19/13

  6. Object-Oriented Programming • Inheritance is a way of defining a new class by extending an existing class with new or changed properties and methods. • The original class is called the parent, super, or base class. PACS – 10/19/13

  7. Object-Oriented Programming • We’ll look at some basic syntax. • Create an object: $object = new class; • Access a property: $object->propertyname • Access a method: $object->methodname([arg,…]) PACS – 10/19/13

  8. Object-Oriented Programming • Methods act just like functions, so they can take arguments and return a value. • You can specify which methods and properties are publicly accessible to provide encapsulation. • Declaring a class: class classname[extends baseclass] { [var$propertyname[ = value];] [functionmethodname(args) { code}] } PACS – 10/19/13

  9. Object-Oriented Programming • Within a method, the $this variable is a reference to the object on which the method was called. • Declaring a method: class Person { public $name; // optional but good practice public function get_name(){ return $this->name;} public function set_name($new_name) { $this->name = $new_name;} } PACS – 10/19/13

  10. Object-Oriented Programming • Advanced – When declaring properties, you can use the following access modifiers instead of varto provide encapsulation: public $data1 [=3]; // global protected $data2 [=4]; // within subclasses private $data3 [=5]; // within class • Advanced – You can also define static properties on variables on an object class. No object necessary to reference these. PACS – 10/19/13

  11. Object-Oriented Programming • Like global constants assigned with the define() function, you can assign constants within a class: class PaymentMethod{ Const TYPE_CREDITCARD = 0; Const TYPE_CASH = 1; } • It’s common practice to define class constants in upper case. PACS – 10/19/13

  12. Object-Oriented Programming • To inherit properties and methods from another class, use the extendskeyword: class Person { public $name, $address, $age; } class Employee extends Person { public $position, $salary; } PACS – 10/19/13

  13. Object-Oriented Programming • If a derived class has a property or method with the same name as in its parent class, the one in the derived class takes precedence. • Advanced - Constructor functions are called when an object is instantiated. • Advanced – Destructor functions are called when the last reference to an object is removed or the script ends. PACS – 10/19/13

  14. Object-Oriented Programming • Advanced – Introspection allows examining an object’s characteristics (for debugging). class_exists(classname) get_declared_classes() get_class_methods(classname) get_class_vars(classname) PACS – 10/19/13

  15. Object-Oriented Programming • Advanced – Serialization allows an object to be converted to a bytestream representation that can be stored in a file. serialize(something) unserialize(something) • This is commonly used with sessions to provide persistence. • Now let’s look at some concrete examples. PACS – 10/19/13

More Related