Adding a property to your class is just as easy as adding a method .You simply declare a variable inside the class to hold the value of the property.
saved as class.demo.php
<?php
class Demo{
public $name; // public means everyone can access this property
function sayHello($name){
print "Hello $this->name!"; /*the variable "name" doesn't need a $ while pointing itself with "$this " */
}
}
?>
saved as testdemo.php
<?php
require_once('class.demo.php');
$objDemo=new Demo(); // objDemo pointed to a object called Demo()
$objDemo->name='Billy'; // assign a name to variable $name
$objAnotherDemo = new Demo();
$objAnotherDemo->name="Ed";
$objDemo->sayHello();// access to the function called sayHello()
$objAnotherDemo->sayHello();
?>
saved as class.demo.php
<?php
class Demo{
public $name; // public means everyone can access this property
function sayHello($name){
print "Hello $this->name!"; /*the variable "name" doesn't need a $ while pointing itself with "$this " */
}
}
?>
saved as testdemo.php
<?php
require_once('class.demo.php');
$objDemo=new Demo(); // objDemo pointed to a object called Demo()
$objDemo->name='Billy'; // assign a name to variable $name
$objAnotherDemo = new Demo();
$objAnotherDemo->name="Ed";
$objDemo->sayHello();// access to the function called sayHello()
$objAnotherDemo->sayHello();
?>
No comments:
Post a Comment