Presentation is loading. Please wait.

Presentation is loading. Please wait.

PACS – 10/19/131 Object-Oriented Programming “Object-oriented programming opens the door to cleaner designs, easier maintenance, and greater code reuseability.”

Similar presentations


Presentation on theme: "PACS – 10/19/131 Object-Oriented Programming “Object-oriented programming opens the door to cleaner designs, easier maintenance, and greater code reuseability.”"— Presentation transcript:

1 PACS – 10/19/131 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.

2 PACS – 10/19/132 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.

3 PACS – 10/19/133 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.

4 PACS – 10/19/134 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.

5 PACS – 10/19/135 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.

6 PACS – 10/19/136 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.

7 PACS – 10/19/137 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,…])

8 PACS – 10/19/138 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];] [function methodname(args) { code }] }

9 PACS – 10/19/139 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;} }

10 PACS – 10/19/1310 Object-Oriented Programming Advanced – When declaring properties, you can use the following access modifiers instead of var to 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.

11 PACS – 10/19/1311 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.

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

13 PACS – 10/19/1313 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.

14 PACS – 10/19/1314 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)

15 PACS – 10/19/1315 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.


Download ppt "PACS – 10/19/131 Object-Oriented Programming “Object-oriented programming opens the door to cleaner designs, easier maintenance, and greater code reuseability.”"

Similar presentations


Ads by Google