60 %
40 %
Information about OOP in PHP

Published on February 10, 2014

Author: sicarii_13

Source: slideshare.net


Object orientation is probably the area that has been subject to the most significant and far-reaching changes with the advent of PHP 5. Rather than making things incompatible with previous versions of the language, however, they enhance PHP 4’s meagre OOP offerings and make PHP 5 a fully functional object-oriented language.

FUNDAMENTALS •Classes •Encapsulation •Objects

CLASS DECLARATION The basic declaration of a class is very simple: class myClass { // Class contents go here }

OBJECT INSTANTIATION This is done by using the new construct: $myClassInstance = new myClass(); An object is always passed by reference rather than by value. $myClassInstance = new myClass(); $copyInstance = $myClassInstance();

CLASS INHERITANCE Allows a class to extend another class, essentially adding new methods and properties, as well as overriding existing ones as needed. class a { function test() { echo "a::test called"; } function func() { echo "a::func called"; } } class b extends a { function test() { echo "b::test called"; } } class c extends b { function test() { parent::test(); } } class d extends c { function test() { b::test(); } } $a = new a(); $b = new b(); $c = new c(); $d = new d();

METHODS AND PROPERTIES Methods are declared just like traditional functions: class myClass { function myFunction() { echo "You called myClass::myFunction"; } } From outside the scope of a class, its methods are called using the indirection operator ->: $obj = new myClass(); $obj->myFunction();

METHODS AND PROPERTIES class myClass { function myFunction($data) { echo "The value is $data"; } function callMyFunction($data) { // Call myFunction() $this->myFunction($data); } } $obj = new myClass(); $obj->callMyFunction(123);

CONSTRUCTORS class foo { function __construct() { echo __METHOD__; } function foo() { // PHP 4 style constructor } } new foo();

DESTRUCTORS class foo { function __construct() { echo __METHOD__ . PHP_EOL; } function __destruct() { echo __METHOD__; } } new foo(); This code will display: foo::__construct foo::__destruct

VISIBILITY public The resource can be accessed from any scope. protected The resource can only be accessed from within the class where it is defined and its descendants. private The resource can only be accessed from within the class where it is defined. The resource is accessible from any scope, but cannot be overridden in descendant classes. final

DECLARING AND ACCESSING PROPERTIES class foo { public $bar; protected $baz; private $bas; public $var1 = "Test"; // String public $var2 = 1.23; // Numeric value public $var3 = array (1, 2, 3); }

CONSTANTS, STATIC METHODS AND PROPERTIES class foo { static $bar = "bat"; public static function baz() { echo "Hello World"; } } $foo = new foo(); $foo->baz(); echo $foo->bar; Hello WorldPHP Strict Standards: Accessing static property foo::$bar as non static in PHPDocument1 on line 17 Strict Standards: Accessing static property foo::$bar as non static in PHPDocument1 on line 1

CLASS CONSTANTS class foo { const BAR = "Hello World"; } echo foo::BAR;

INTERFACES AND ABSTRACT CLASSES •An abstract class essentially defines the basic skeleton of a specific type of encapsulated entity. •Interfaces, on the other hand, are used to specify an API that a class must implement.

EXCEPTIONS • Exceptions provide an error control mechanism that is more fine-grained than traditional PHP fault handling, and that allows for a much greater degree of control. • Key differences between “regular” PHP errors and exceptions: • Exceptions are objects, created (or “thrown”) when an error occurs • Exceptions can be handled at different points in a script’s execution, and different types of exceptions can be handled by separate portions of a script’s code • All unhandled exceptions are fatal • Exceptions can be thrown from the __construct method on failure • Exceptions change the flow of the application

Add a comment

Related presentations

Related pages

PHP: Classes and Objects - Manual

PHP 5 is very very flexible in accessing member variables and member functions. These access methods maybe look unusual and unnecessary at first glance ...
Read more

OOP in PHP - Teil 1: Grundlagen - Ab Heute Programmieren

OOP steht für Objektorientierte Programmierung. Doch was bedeutet dieser Begriff? In dieser Serie sehen wir uns die OOP Umsetzung von PHP an.
Read more

Object Oriented Programming in PHP - Tutorials Point

Object Oriented Programming in PHP - A simple and short PHP tutorial and complete reference manual for all built-in PHP functions. This tutorial is ...
Read more

Objektorientierte Programmierung in PHP - PHP-Kurs.com ...

Objektorientierte Programmierung in PHP. Objektorientierte Programmierung zu verstehen ist nicht das einfachste. Bisher haben wir prozedural programmiert.
Read more

Object-Oriented PHP for Beginners - Learn How-to Code by ...

Object-Oriented PHP for Beginners. by Jason Lengstorf 23 Dec 2011. ... For information on OOP compatibility with PHP 4, see the PHP manual page.
Read more

Object Oriented PHP for Beginners – KillerPHP.com

Learn to build object oriented PHP apps with my NEW interactive ... You just found the easiest to understand tutorial out there on OOP and PHP. It may sound
Read more

Object Oriented Programming with PHP - PHPRO.ORG

by Kevin Waterson. Draft Only. Contents. What is OOP; What is an Object; What is a class; Commenting code; Inheritance (Extending a class) Visibility ...
Read more

OOP in PHP - Teil 4: Klonen und Typprüfung - Ab Heute ...

Um möglichst fehlerarmen Code zu schreiben müssen wir uns in PHP mit den richtigen Werten und Typen von Variablen herumschlagen. Doch wie geht das bei OOP?
Read more

Websiteentwicklung: PHP: OOP – Wikibooks, Sammlung ...

Was ist OOP? OOP bedeutet Objektorientiertes Programmieren, was aber wohl noch nicht den gewünschten "Aha"-Effekt hervorrufen sollte. Was ist also ein Objekt?
Read more

PHP: The Basics - Manual - PHP: Hypertext Preprocessor

stdClass is the default PHP object. stdClass has no properties, methods or parent. It does not support magic methods, and implements no interfaces.
Read more