Learning PHP and MySQL

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) TrackbacksPermalink

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>/";
?>

Posted by krautgrrl on 08/03 at 08:24 AM
Chapter 5 Code • (0) Comments • (0) TrackbacksPermalink

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();
  
}
}
?>

Posted by krautgrrl on 08/03 at 08:25 AM
Chapter 5 Code • (0) Comments • (0) TrackbacksPermalink

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);
  
}
}
?>

Posted by krautgrrl on 08/03 at 08:25 AM
Chapter 5 Code • (2) Comments • (0) TrackbacksPermalink

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();
?>

Posted by krautgrrl on 08/03 at 08:26 AM
Chapter 5 Code • (0) Comments • (2) TrackbacksPermalink

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) TrackbacksPermalink

Monday, August 07, 2006

Example 06-01 Using the array function to create an array of weekdays

<?php
$weekdays
=array('Monday',
                
'Tuesday',
                
'Wednesday',
                
'Thursday',
                
'Friday',
                
'Saturday',
                
'Sunday');
?>

Posted by krautgrrl on 08/07 at 10:17 AM
Chapter 6 Code • (0) Comments • (0) TrackbacksPermalink

Example 06-02 Creating an associative array of shapes

<?php
$shapes
=array('Soda Can'  =>  'Cylinder',
              
'Note Pad'  =>  'Rectangle',
              
'Apple'  =>  'Sphere',
              
'Orange'  =>  'Sphere',
              
'Phonebook'  =>  'Rectangle');
?>

Posted by krautgrrl on 08/07 at 10:18 AM
Chapter 6 Code • (0) Comments • (0) TrackbacksPermalink

Example 06-03 Displaying one value from an array

<?php
$shapes
=array('Soda Can'  =>  'Cylinder',
              
'Note Pad'  =>  'Rectangle',
              
'Apple'  =>  'Sphere',
              
'Orange'  =>  'Sphere',
              
'Phonebook'  =>  'Rectangle');
print  
"A  note  pad  is  a  {$shapes['Note Pad']}.";
?>

Posted by krautgrrl on 08/07 at 10:19 AM
Chapter 6 Code • (0) Comments • (2) TrackbacksPermalink

Example 06-04 Display the contents of an array using a loop

<?php
$shapes
=array('Soda Can'  =>  'Cylinder',
              
'Note Pad'  =>  'Rectangle',
              
'Apple'  =>  'Sphere',
              
'Orange'  =>  'Sphere',
              
'Phonebook'  =>  'Rectangle');
foreach (
$shapes  as  $key  =>  $value)  {
  
print"The $key is a $value.<br>\n";
}
?>

Posted by krautgrrl on 08/07 at 10:20 AM
Chapter 6 Code • (0) Comments • (1) TrackbacksPermalink

Example 06-05 Counting the elements in an array

<?php
$shapes
=array('Soda Can'  =>  'Cylinder',
              
'Note Pad'  =>  'Rectangle',
              
'Apple'  =>  'Sphere',
              
'Orange'  =>  'Sphere',
              
'Phonebook'  =>  'Rectangle');
$numElements  =  count($shapes);
print
"The array has $numElements elements.<br>\n";
?>

Posted by krautgrrl on 08/07 at 10:20 AM
Chapter 6 Code • (0) Comments • (0) TrackbacksPermalink

Example 06-06 Using sort to alphabetize

<?php
$shapes  
=  array("rectangle""cylinder""sphere");
sort($shapes);
//The  foreach  loop  selects  each  element  from  the  array  and  assigns  its  value  to  $key
//before  executing  the  code  in  the  block. foreach ($shapes  as  $key  =>  $val)  {
echo "shapes[" $key "]  =  " $val "<br>";
}
?>

Posted by krautgrrl on 08/07 at 10:21 AM
Chapter 6 Code • (1) Comments • (6) TrackbacksPermalink

Example 06-07 Creating a multidimensional array

<?php
$objects
=array('Soda Can' => array('Shape' => 'Cylinder',
                                    
'Color' =>  'Red',
                                    
'Material' => 'Metal'),
               
'Note Pad' => array('Shape' => 'Rectangle',
                                   
'Color' => 'White',
                                   
'Material' => 'Paper'),
               
'Apple' => array('Shape' => 'Sphere',
                                
'Color' => 'Red',
                                
'Material' => 'Fruit'),
               
'Orange' => array('Shape' => 'Sphere',
                                 
'Color' => 'Orange',
                                 
'Material'  =>  'Fruit'),
               
'Phonebook' => array('Shape' => 'Rectangle',
                                    
'Color' => 'Yellow',
                                    
'Material' => 'Paper'));
echo 
$objects['Soda Can']['Shape'];
?>

Posted by krautgrrl on 08/07 at 10:23 AM
Chapter 6 Code • (0) Comments • (1) TrackbacksPermalink

Example 06-08 Displaying a multidimensional array

<?php
foreach ($objects as $obj_key => $obj)
{
  
echo "$obj_key:<br>";
  while (list (
$key,$value)=each ($obj))
  
{
    
echo "$key = $value ";
  
}
  
echo "<br>";
}
?>

Posted by krautgrrl on 08/07 at 10:24 AM
Chapter 6 Code • (0) Comments • (3051) TrackbacksPermalink

Example 06-09 Using extract on an associative array

<?php
$shapes
=array('SodaCan'  =>  'Cylinder',
              
'NotePad'  =>  'Rectangle',
              
'Apple'  =>  'Sphere',
              
'Orange'  =>  'Sphere',
              
'PhoneBook'  =>  'Rectangle');

extract($shapes);
//  $SodaCan,  $NotePad,  $Apple,  $Orange,  and  $PhoneBook  are  now  set echo  $Apple;
echo "<br>";
echo 
$NotePad;
?>

Posted by krautgrrl on 08/07 at 10:24 AM
Chapter 6 Code • (0) Comments • (1) TrackbacksPermalink
Page 5 of 15 pages « First  <  3 4 5 6 7 >  Last »

Statistics

This page has been viewed 374214 times
Page rendered in 0.2098 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: 13
Total anonymous users: 0
Most Recent Visitor on: 02/06/2012 06:19 am
The most visitors ever was 1103 on 11/20/2007 12:50 pm

Referrers

Powered by ExpressionEngine