PHP/Class/Class Method
Содержание
- 1 Accessing the Attributes of a Class by Using Functions
- 2 Access properties
- 3 A Class with a Method
- 4 Call class methods directly
- 5 Calling an Overridden Method (PHP 5 Syntax)
- 6 Class Member and Method Definitions
- 7 Class Member Overloading
- 8 Defining three member functions for Cat
- 9 Overriding Methods
- 10 Pass class instance as parameter
- 11 The Method of a Child Class Overriding That of Its Parent (PHP 4 Syntax)
Accessing the Attributes of a Class by Using Functions
<?php
class employee{
var $emp_code;
var $name;
var $address;
var $department;
var $sex;
var $date_of_birth;
var $salary;
function showsalary () {
echo "Your salary is :",$this->salary," \n";
}
}
$dave = new employee;
$dave->emp_code="8";
$dave->name="Dave";
$dave->address="Apartment 1";
$dave->department="Admin Development";
$dave->sex="Male";
$dave->salary="70";
$dave->date_of_birth="15-09-1977";
$dave->showsalary();
?>
Access properties
<?php
class Bird
{
function __construct($name="No-name", $breed="unknown", $price = 15)
{
$this->name = $name;
$this->breed = $breed;
$this->price = $price;
}
function setName($name)
{
$this->name = $name;
}
function setBreed($breed)
{
$this->breed = $breed;
}
function setPrice($price)
{
$this->price = $price < 0 ? 0 : $price;
}
function getName()
{
return $this->name;
}
function getBreed()
{
return $this->breed;
}
function getPrice()
{
return $this->price;
}
function display()
{
printf("<p>%s is a %s, and costs \$%.2f.</p>\n",
$this->name, $this->breed, $this->price);
}
}
$magpie = new Bird("Malaysia", "magpie", 7.5);
$magpie->display();
$magpie->setPrice(-14.95);
$magpie->display();
$magpie->price = -14.95;
$magpie->display();
?>
A Class with a Method
<?php
class Item {
var $name = "item";
function getName() {
return "item";
}
}
$item = new Item ();
print $item->getName ();
?>
Call class methods directly
<?php
class Visitors
{
public function greetVisitor()
{
echo "Hello<br />";
}
function sayGoodbye()
{
echo "Goodbye<br />";
}
}
Visitors::greetVisitor();
$visitor = new Visitors();
$visitor->sayGoodbye();
?>
Calling an Overridden Method (PHP 5 Syntax)
<?php
class Item {
private $name;
function __construct( $name="item", $code=0 ) {
$this->name = $name;
$this->code = $code;
}
function getName() {
return $this->name;
}
}
class PriceItem extends Item {
function getName() {
return "(price) ".parent::getName ();
}
}
$item = new PriceItem ("widget", 5442);
print $item->getName();
?>
Class Member and Method Definitions
Name Description
Const Defines a constant member.
Public Accessible from any object of the class.
Protected Accessible from the class where it is defined and from inherited classes.
Private Accessible from the class where it is defined.
Static Modifier. When used alone, public is assumed.
<?php
class myclass {
public $a;
function set_value($val) {
$this->a = $val;
}
}
$obj = new myclass;
$obj->set_value(123);
echo "Member a = $obj->a\n";
$obj->a = 7;
echo "Member a = $obj->a\n";
?>
Class Member Overloading
<?php
class ParentClass {
public function callMe() {
echo "Parent called!\n";
}
}
class ChildClass extends ParentClass {
public function callMe() {
echo "Child called!\n";
}
}
$child = new ChildClass;
$child->callMe();
?>
Defining three member functions for Cat
<?php
Class Cat {
// Constructor
function __constructor( ) {
}
// The cat meows
function meow( ) {
echo "Meow...";
}
// The cat eats
function eat( ) {
echo "*eats*";
}
// The cat purrs
function purr( ) {
echo "*Purr...*";
}
}
?>
Overriding Methods
<?
class Dog {
public function bark( ) {
print "Woof!\n";
}
}
class Poodle extends Dog {
public function bark( ) {
print "Yip!\n";
}
}
?>
Pass class instance as parameter
<?php
class Employee {
public $title;
public $lastName;
public $firstName;
public $price;
function __construct( $title, $firstName, $mainName, $price ) {
$this->title = $title;
$this->firstName = $firstName;
$this->lastName = $mainName;
$this->price = $price;
}
function getFullName() {
return "{$this->firstName}" . " {$this->lastName}";
}
}
class EmployeeWriter {
public function write( $shopProduct ) {
$str = "{$shopProduct->title}: ";
$str .= $shopProduct->getFullName();
$str .= " ({$shopProduct->price})\n";
print $str;
}
}
$product1 = new Employee( "Title", "A", "B", 5.9 );
$writer = new EmployeeWriter();
$writer->write( $product1 );
?>
The Method of a Child Class Overriding That of Its Parent (PHP 4 Syntax)
<?php
class Item {
var $name;
function Item( $name="item", $code=0) {
$this->name = $name;
$this->code = $code;
}
function getName() {
return $this->name;
}
}
class PriceItem extends Item {
function getName() {
return "(price).".$this->name;
}
}
$item = new PriceItem( "widget", 5442 );
print $item->getName();
?>