Learning PHP and MySQL
Chapter 5 Code
Thursday, August 03, 2006
Example 05-16 Accessing the $age variable using this-->
<?php
class Cat {
// How old the cat is var $age;
// Constructor
function Cat($new_age)
{
// Set the age of this cat to the new age
$this->age = $new_age;
}
//The birthday method increments the age variable
function Birthday()
{
$this->age++;
}
}
// Create a new instance of the cat object that’s one year old
$fluffy=new Cat(1);
echo "Age is $fluffy->age <br/>";
echo "Birthday<br/>";
// Increase fluffy’s age
$fluffy->Birthday();
echo "Age is $fluffy->age <br/>";
?>
Posted by krautgrrl on 08/03 at 08:24 AM
Chapter 5 Code • (0) Comments • (79) Trackbacks • Permalink
Chapter 5 Code • (0) Comments • (79) Trackbacks • Permalink
Example 05-17 Using the extends keyword to define a subclass
<?php
class Cat {
// How old the cat is var $age;
function Cat($new_age)
{
// Set the age of this cat to the new age
$this->age = $new_age;
}
function Birthday()
{
$this->age++;
}
}
class Domestic_Cat extends Cat {
// Constructor
function Domestic_Cat() {
}
// Sleep like a domestic cat
function sleep() {
echo("Zzzzzz.<br/>");
}
}
$fluffy=new Domestic_Cat();
$fluffy->Birthday();
$fluffy->sleep();
echo "Age is $fluffy->age <br>/";
?>
Example 05-18 Using the parent construct
<?php
class Cat {
// How old the cat is var $age;
function Cat($new_age)
{
// Set the age of this cat to the new age
$this->age = $new_age;
}
function Birthday()
{
$this->age++;
}
function Eat()
{
echo "Chomp chomp.";
}
function Meow()
{
echo "Meow.";
}
}
class Domestic_Cat extends Cat {
// Constructor
function Domestic_Cat() {
}
// Eat like a Domestic_Cat
function eat() {
parent::eat();
// After we're finished eating, let's meow
$this->meow();
}
}
?>
Example 05-19 Calling the constructor of the parent class
<?php
class Cat {
// How old the cat is var $age;
function Cat($new_age)
{
// Set the age of this cat to the new age
$this->age = $new_age;
}
function Birthday()
{
$this->age++;
}
function Eat()
{
echo "Chomp chomp.";
}
function Meow()
{
echo "Meow.";
}
}
class Domestic_Cat extends Cat {
// Constructor
function Domestic_Cat($new_age) {
// This will call the constructor
// in the parent class (the superclass)
parent::Cat($new_age);
}
}
?>
Example 05-20 Using the -> operator to call hypnotize
<?php
class Hypnotic_Cat extends Cat {
// Constructor
function Hypnotic_Cat() {
}
// This is meant to be called statically function
hypnotize() {
//detects that the function is being called statically
//since a static call doesn’t have an object to point to
if ($this == null)
echo ("All cats are hypnotized.");
else
{
echo ("The cat was hypnotized.");
return;
}
}
}
// Hypnotize all cats
Hypnotic_Cat::hypnotize();
$hypnotic_cat = new Hypnotic_Cat();
// Does nothing
$hypnotic_cat->hypnotize();
?>
Example 05-21 Referencing the $some_variable
<?php
$some_variable = "Hello World!";
$some_reference = &$some_variable;
$some_reference = "Guten Tag World!";
echo $some_variable;
echo $some_reference;
?>
Posted by krautgrrl on 08/03 at 08:26 AM
Chapter 5 Code • (0) Comments • (810) Trackbacks • Permalink
Chapter 5 Code • (0) Comments • (810) Trackbacks • Permalink
Question 05-01 What’s wrong with this function call?
This code is for Question 5-1.
<?php
// define a function
function Response {
echo "Have a good day!<br/><br/>";
}
// driving to work
echo "Are you going to merge? <br/>";
Response;
// at the office
echo "I need a status report on all your projects in the next 10 minutes for my management meeting.<br/>";
Response;
// at the pub after work
echo "Did Bill get everything he needed today? He was sure crabby!<br/>";
Response;
?>
Statistics
This page has been viewed 375192 times
Page rendered in 0.2244 seconds
Total Entries: 224
Total Comments: 16
Total Trackbacks: 307338
Most Recent Entry: 09/27/2006 12:39 pm
Most Recent Comment on: 10/26/2007 10:00 am
Total Members: 2
Total Logged in members: 0
Total guests: 11
Total anonymous users: 0
Most Recent Visitor on: 02/10/2012 09:24 am
The most visitors ever was 1103 on 11/20/2007 12:50 pm
