PHP/Class/Constructor — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 10:00, 26 мая 2010
Содержание
- 1 A Class with a Constructor
- 2 Adding a Constructor to PriceItem
- 3 Calling the constructor of the parent class
- 4 Constructors and Destructors
- 5 Creating the Cat constructor
- 6 Define and use constructor
- 7 Define class as constructor parameter
- 8 Define Constructor for Class
- 9 Defining an object constructor
- 10 Defining object constructors in PHP 4
- 11 Instantiate class by calling the constructor
- 12 invoking parent constructors
- 13 Using Default Constructors
- 14 Using the PHP 5 style constructor
- 15 Using Unified Constructors and Destructors
A Class with a Constructor
<source lang="html4strict">
<?php
class Item { var $name; function Item( $name="item") { $this->name = $name; } function setName( $n) { $this->name = $n; } function getName () { return $this->name; } } $item = new Item("5"); print $item->getName ();
?>
</source>
Adding a Constructor to PriceItem
<source lang="html4strict">
<?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 {
private $price; function __construct( $name, $code, $price ) { parent::__construct( $name, $code ); $this->price = $price; } function getName() { return "(price) ".parent::getName (); }
} $item = new PriceItem ("widget", 5442, 5.20); print $item->getName (); ?>
</source>
Calling the constructor of the parent class
<source lang="html4strict">
<?php
class Cat { var $age; function Cat($new_age){ $this->age = $new_age; } function Birthday( ){ $this->age++; } function Eat( ){ echo "Chomp chomp."; } function Meow( ){ echo "Meow."; }
} class MyCat extends Cat {
function MyCat($new_age) { parent::Cat($new_age); }
} ?>
</source>
Constructors and Destructors
<source lang="html4strict">
<?php
class SimpleClass { function SimpleClass($param) { echo "Created a new instance of SimpleClass!"; } } $myinstance = new SimpleClass;
?>
</source>
Creating the Cat constructor
<source lang="html4strict">
<?php class Cat {
function Cat( ) { }
} ?>
</source>
Define and use constructor
<source lang="html4strict">
<?php
class book { private $title; private $isbn; private $copies; public function __construct($isbn) { $this->setIsbn($isbn); $this->getTitle(); $this->getNumberCopies(); } public function setIsbn($isbn) { $this->isbn = $isbn; } public function getTitle() { $this->title = "title"; print "Title: " . $this->title . "
"; } public function getNumberCopies() { $this->copies = "5"; print "Number copies available: " . $this->copies."
"; } } $book = new book("1111");
?>
</source>
Define class as constructor parameter
<source lang="html4strict">
<?php class PersonWriter {
function writeName( Person $p ) { print $p->getName()."\n"; } function writeAge( Person $p ) { print $p->getAge()."\n"; }
} class Person {
private $writer; function __construct( PersonWriter $writer ) { $this->writer = $writer; } function __call( $method, $args ) { if ( method_exists( $this->writer, $method ) ) { return $this->writer->$method( $this ); } } function getName() { return "Joe"; } function getAge() { return 44; }
} $person= new Person( new PersonWriter() ); $person->writeName(); $person->writeAge(); ?>
</source>
Define Constructor for Class
<source lang="html4strict">
<?php class employee { var $emp_code; var $name; var $address; var $department; var $sex; var $date_of_birth; var $salary; function employee($empname) { $this->emp_code="default value"; $this->name=$empname; $this->address="default value"; $this->department="default value"; $this->sex="default value"; $this->date_of_birth="default value"; $this->salary="default value"; } } $dave = new employee("Dave Osbourne"); echo "Employee Code:",$dave->emp_code," \n"; echo "Name:",$dave->name," \n"; echo "Address:",$dave->address," \n"; echo "Department:",$dave->department," \n"; echo "Sex:",$dave->sex," \n"; echo "Salary:",$dave->salary," \n"; echo "Date of Birth:",$dave->date_of_birth," \n"; ?> </source>
Defining an object constructor
<source lang="html4strict">
class user {
public $username; function __construct($username, $password) { if ($this->validate_user($username, $password)) { $this->username = $username; } }
} $user = new user("A", "M");
</source>
Defining object constructors in PHP 4
<source lang="html4strict">
class user {
function user($username, $password) { }
}
</source>
Instantiate class by calling the constructor
<source lang="html4strict">
<?php
class Staff { private $ein; function __construct($ein) { if ($this->verify_ein($ein)) { echo "called"; } } protected function verify_ein($ein) { return TRUE; } } $employee = new Staff("123");
?>
</source>
invoking parent constructors
<source lang="html4strict">
<?php class Staff {
protected $name; protected $title; function __construct() {echo "
Staff constructor called!
";}
} class Manager extends Staff {
function __construct() { parent::__construct();echo "
Manager constructor called!
";}
} $employee = new Manager(); ?>
</source>
Using Default Constructors
<source lang="html4strict">
<?php class Dog {
function __construct($name="No-name", $breed="breed unknown", $price = 15) { $this->name = $name; $this->breed = $breed; $this->price = $price; }
} $aDog = new Dog(); $tweety = new Dog("A", "a");
printf("%s is a %s and costs \$%.2f.
\n",$aDog->name, $aDog->breed, $aDog->price); $tweety->price = 24.95;
printf("%s is a %s and costs \$%.2f.
\n",$tweety->name, $tweety->breed, $tweety->price); ?>
</source>
Using the PHP 5 style constructor
<source lang="html4strict">
<?php class Cat {
Function __constructor( ){ }
} ?>
</source>
Using Unified Constructors and Destructors
<source lang="html4strict">
<?php
class SimpleClass { function __construct($param) { echo "Created a new instance of SimpleClass!"; } function __destruct() { echo "Destroyed this instance of SimpleClass"; } } $myinstance = new SimpleClass("value"); unset($myinstance);
?>
</source>