lecture5

lecture5 - CSE 135 Server Side Web Languages Lecture # 5...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
CSE 135 Server Side Web Languages Lecture # 5 PHP Part II Friday, August 6, 2010
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 135 Server Side Web Languages Lecture # 5 Classes • In PHP, we define a class with the ‘class’ keyword followed by the class name: class BankAccount{ } • When we use the ‘class’ keyword, we are creating a datatype. In order to use the type, we must create a variable with the ‘new’ operator. $myAccount = new BankAccount(); Friday, August 6, 2010
Background image of page 2
CSE 135 Server Side Web Languages Lecture # 5 Properties and Methods A class can contain properties and methods. Our BankAccount example will need to have a balance and a way to change the balance. class BankAccount{ var $balance; function setBalance($balance){ $this->balance = $balance; } function getBalance(){ return $this->balance; } } In this example, the $balance variable is a property (unfortunately public here) and setBalance() and getBalance() are the methods. Friday, August 6, 2010
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 135 Server Side Web Languages Lecture # 5 Methods and $this We can call setBalance after we create an instance of BankAccount. $myAccount = new BankAccount(); $ m y A c c o u n t - > s e t B a l a n c e ( 1 0 0 ) ; In the setBalance() method, we see the keyword ‘$this’ function setBalance($balance){ $this->balance = $balance; } $this is a reference to the calling object. In our example above, $this would be pointing to the $myAccount object. This allows us to later reference the class property through another method call. $myAccount = new BankAccount(); $ m y A c c o u n t - > s e t B a l a n c e ( 1 0 0 ) ; echo $myAccount->getBalance(); //prints 100 Friday, August 6, 2010
Background image of page 4
CSE 135 Server Side Web Languages Lecture # 5 Constructors • Constructors are optional in PHP classes. • If a constructor is defined, it will always be called when an object is created. • A constructor is declared by creating a class method called __construct. function __construct(){ $this->balance = 50; } • There can only be one constructor per class. Friday, August 6, 2010
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 135 Server Side Web Languages Lecture # 5 Constructors Cont. • It is also possible to pass a constructor a variable. function __construct($start){ $this->balance = $start; } • When creating the object, we would pass a value to the new operator. $myAccount = new BankAccount(25); Friday, August 6, 2010
Background image of page 6
CSE 135 Server Side Web Languages Lecture # 5 Inheritance • One important feature of OOP is inheritance. PHP supports inheritance with the “extends” keyword. class CheckingAccount extends BankAccount{ var $pin; f u n c t i o n s e t P i n ( $ p i n ) { $ t h i s - > p i n = $ p i n ; } function getPin(){ r e t u r n $ t h i s - > p i n ; } } Friday, August 6, 2010
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CSE 135 Server Side Web Languages Lecture # 5 Inheritance Cont. • Now, any object of the type CheckingAccount will have
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 50

lecture5 - CSE 135 Server Side Web Languages Lecture # 5...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online