Object-Oriented Programming with PHP (part 1)
-
Upload
bozhidar-boshnakov -
Category
Software
-
view
153 -
download
0
Transcript of Object-Oriented Programming with PHP (part 1)
Object-Oriented Programming with PHPPart 1
by Bozhidar Boshnakov
Recap• We talked about
– Loops– Conditional statements– Functions and return values– Include and require– Variables scope
Table of contents
• Classes and objects• Methods and properties• Scope
• 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
• 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
}
• 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!";
• 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'
• 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();
• 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;
• 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';
• 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');
• 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
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;
• Resources– http://php-uroci.devbg.org/– http://academy.telerik.com/– http://www.codecademy.com/