Object-Oriented Programming with PHP (part 1)

15
Object-Oriented Programming with PHP Part 1 by Bozhidar Boshnakov

Transcript of Object-Oriented Programming with PHP (part 1)

Page 1: Object-Oriented Programming with PHP (part 1)

Object-Oriented Programming with PHPPart 1

by Bozhidar Boshnakov

Page 2: Object-Oriented Programming with PHP (part 1)

Recap• We talked about

– Loops– Conditional statements– Functions and return values– Include and require– Variables scope

Page 3: Object-Oriented Programming with PHP (part 1)

Table of contents

• Classes and objects• Methods and properties• Scope

Page 4: Object-Oriented Programming with PHP (part 1)

• The idea of Object Oriented Programming is to move the architecture of an application closer to real world– Classes are types of entities– Objects are single units of a given class– Example – Dog is a class, your dog Lassie is an

object of class Dog– Classes have methods and properties– Classes and objects help to create well-structured

application

Page 5: Object-Oriented Programming with PHP (part 1)

• Declaring of a class in PHP can be done anywhere in the code

– Two special methods: constructor and destructor• Executed when creating or destroying new object of

this class• Used to initialize or cleanup properties and etc.

class Dog {… // declare methods and properties

}

Page 6: Object-Oriented Programming with PHP (part 1)

• Class definition begins with the class keyword, followed by its name and methods and properties list

– Objects of class (instances) are created with the keyword new

class A {function foo () {

echo "foo here!";}

}$myFirstObject = new A();$myFirstObject->foo(); // prints out "foo here!";

Page 7: Object-Oriented Programming with PHP (part 1)

• Each class can have only one constructor

– All parameters of the creating of the object are passed to the constructor

class A {function __construct ($bar) {

echo $bar;}function foo () {

echo "foo here!";}

}$myFirstObject = new A('test'); // print 'test'

Page 8: Object-Oriented Programming with PHP (part 1)

• Class can have unlimited number of properties

• The $this variable points to the current object – called execution context

class A {var $bar;function __construct ($bar) {

$this->bar = $bar;}function myPrint () {

echo $this->bar;}}$myFirstObject = new A('test'); $myFirstObject->myPrint();

Page 9: Object-Oriented Programming with PHP (part 1)

• Class can specify default value for a property

• Properties can be accessed from the outside world

class A {var $bar = 'default value';…

class A {var $bar = 'default value';…

}$obj = new A;echo $obj->bar;

Page 10: Object-Oriented Programming with PHP (part 1)

• Example of what $this is

• Can be used to access methods too

class A {var $bar;function __construct ($bar) {

$this->bar = $bar;}function myPrint () {

echo $this->bar;}}$myFirstObject = new A('test'); $myFirstObject->myPrint(); // prints 'test'$anotherObject = new A('foo'); $anotherObject ->myPrint(); // prints 'foo';

Page 11: Object-Oriented Programming with PHP (part 1)

• Each class can have only one destructor– Must be public

– Destructors are automatically called when script is shutting down

class A {function __construct ($name) {

$this->fp = fopen ($name, 'r');}function __destruct () {

fclose($this->fp);}

}$myFirstObject = new A('test');

Page 12: Object-Oriented Programming with PHP (part 1)

• Each method and property has a scope– It defines who can access it– Three levels – public, protected, private• Private can be access only by the object itself • Protected can be accessed by descendant classes (see

inheritance)• Public can be accessed from the outside world

– Level is added before function keyword or instead of var• var is old style (PHP 4) equivalent to public

– Constructors always need to be public

Page 13: Object-Oriented Programming with PHP (part 1)

class A {private $bar;public function __construct ($bar) {

$this->bar = $bar;}public function myPrint () {

echo $this->bar;}}

$myFirstObject = new A('test'); $myFirstObject->myPrint(); // prints 'test'// this will not work:echo $myFirstObject->bar;

Page 14: Object-Oriented Programming with PHP (part 1)

• Resources– http://php-uroci.devbg.org/– http://academy.telerik.com/– http://www.codecademy.com/

Page 15: Object-Oriented Programming with PHP (part 1)