{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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
Image of page 1

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

View Full Document Right 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
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
Image of page 3

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

View Full Document Right 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(); $myAccount->setBalance(100); 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(); $myAccount->setBalance(100); echo $myAccount->getBalance(); //prints 100 Friday, August 6, 2010
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
Image of page 5

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

View Full Document Right 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
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; function setPin($pin){ $this->pin = $pin; } function getPin(){ return $this->pin; } } Friday, August 6, 2010
Image of page 7

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

View Full Document Right Arrow Icon
CSE 135 Server Side Web Languages Lecture # 5 Inheritance Cont. Now, any object of the type CheckingAccount will have two class properties: pin and balance. $myAccount = new CheckingAccount(0); $myAccount->setBalance(100); $myAccount->setPin("3333"); Note: Even though the CheckingAccount class does not have a constructor, we pass in a value to the new operator. This is because the BankAccount constructor will be used.
Image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern