/** * Created by PhpStorm. * User: roger * Date: 4/10/15 * Time: 12:25 */ class Person { public $name; public $age; public function __construct($name) { $this->name = $name; } public function getAge($age) { return $this->age * 365; } public function setAge($age) { if ($age < 18) { throw new Exception("Es menor d'edat"); } $this->age = $age; } } $john = new Person('John Doe'); $john->setAge(30); $john->age = 3; var_dump($john->getAge());
<?php class Person { public $age; public $name; public function __construct($name) { $this->name = $name; } public function setAge($age) { if ($age < 18) { throw new exception("Person is not old enough"); } $this->age = $age; } public function getAge($age) { return $age * 365; } } $john = new Person("John Doe"); $john->setAge(30); echo "<pre>"; var_dump($john->getAge(30)); echo "</pre>";
<?php /** * Created by PhpStorm. * User: Jensenkong * Date: 2015/8/9 * Time: 10:10 */ require_once 'Person.php'; $m2 = new Person("Jensenkong", 27); echo $m2->getName() . $m2->getAge() . '</br>'; echo $m2->getFile(); echo $m2->getDir();
/** * Created by PhpStorm. * User: oscar * Date: 08/10/2015 * Time: 12:54 */ class Person { private $name; private $age; public function __construct($name) { $this->name = $name; } public function getAge() { return $this->age * 365; } public function setAge($age) { if ($age < 18) { throw new Exception("Es menor d'edat"); } $this->age = $age; } } $Person = new Person('Pepe pepito'); $Person->setAge(19); var_dump($Person->getAge());
// parent::__construct(); echo "子类的构造函数被加载了"; } function __destruct() { echo "对象被销毁的时候我就会被执行"; } // 子类调用父类的静态方法 public static function start() { parent::SpeedUp(); } } // 创建父类 $p1 = new Person("jack", 23); echo $p1->getAge() . '<br/>'; Person::showNation(); var_dump($p1); // 调用父类的静态方法 Person::getSpeed(); Person::SpeedUp(); // 调用受保护的方法 echo $p1->showList(); // 创建子类 $a1 = new Actor(); var_dump($a1); // unset($a1); // 调用子类的静态方法 Actor::start(); /* 如果构造函数定义成了私有方法,则不允许直接实例化对象了,这时候一般通过静态方法进行实例化, 在设计模式中会经常使用这样的方法来控制对象的创建,比如单例模式只允许有一个全局唯一的对象
function writeAge(Person $p) { print $p->getAge() . "\n"; }
<?php /** * Author: ryo * Date: 2012/11/18 * Time: 14:53 */ require 'Person.class.php'; $obj = new Person('Shoin Yoshida', 'male', '1830/9/20'); echo $obj->getAge() . "歳\n"; echo $obj->name; //echo $obj->_gender;
/** * Tests Person->getAge() */ public function testGetAge() { $this->assertEquals('AGE', $this->Person->getAge()); }
<?php require_once dirname(__FILE__) . '/Person1.php'; $ivan = new Person('Иван', 25); $maria = new Person('Мария', 33); $ivan->greet(); $maria->greet(); echo $ivan->getAge(); echo "<br>"; echo Person::getPeople();
<?php class Person { public $name; public $age; public function __construct($name) { $this->name = $name; } public function getAge() { return $this->age * 365; } public function setAge($age) { if ($age < 18) { throw new Exception("Person is not old enough."); } $this->age = $age; } } $steven = new Person("Steven Jasionowicz"); $steven->setAge(26); var_dump($steven->getAge());