Connecting Tech Pros Worldwide Forums | Help | Site Map

Simple example of how to wirite php using classes

Member
 
Join Date: Aug 2007
Posts: 120
#1: Aug 24 '07
hi,

hello friends, i know basic php, i want to use advanced php classes is anybody knows how to write simple example using php classes.

i want to use this one for listbox::onChange class.

regards
shiva

Atli's Avatar
Moderator
 
Join Date: Nov 2006
Location: Iceland
Posts: 3,754
#2: Aug 24 '07

re: Simple example of how to wirite php using classes


Hi.

Check out Classes and Objects at the PHP.net Manual.

The class you mentioned, listbox, does not exists in PHP.
It looks more like a .Net class. Where did you find it?
gregerly's Avatar
Expert
 
Join Date: Sep 2006
Posts: 189
#3: Aug 24 '07

re: Simple example of how to wirite php using classes


VERY SIMPLE CLASS

class.php

[PHP]class myClass{

var $message;

function echoMessage(){
echo $this->message;
}

}[/PHP]

index.php

[PHP]include("./path/to/the/class.php");
$class=new myClass;

$class->message = "Hello World";
$class->echoMessage();

//Results in "Hello World"[/PHP]

This is an extremely simple example of OOP and using classes in PHP.

Greg
pbmods's Avatar
Site Moderator
 
Join Date: Apr 2007
Location: Texas
Posts: 5,435
#4: Aug 24 '07

re: Simple example of how to wirite php using classes


Or in PHP 5:

Expand|Select|Wrap|Line Numbers
  1. class myClass
  2. {
  3.      protected $message;
  4.  
  5.      public function __construct()
  6.      {
  7.           $this->message = 'Hello, World!';
  8.      } 
  9.  
  10.      public function echoMessage()
  11.      {
  12.           echo $this->message;
  13.      }
  14. }
  15.  
gregerly's Avatar
Expert
 
Join Date: Sep 2006
Posts: 189
#5: Aug 24 '07

re: Simple example of how to wirite php using classes


Quote:

Originally Posted by pbmods

Or in PHP 5:

Expand|Select|Wrap|Line Numbers
  1. class myClass
  2. {
  3.      protected $message;
  4.  
  5.      public function __construct()
  6.      {
  7.           $this->message = 'Hello, World!';
  8.      } 
  9.  
  10.      public function echoMessage()
  11.      {
  12.           echo $this->message;
  13.      }
  14. }
  15.  

well done good sir :)
Reply