php OOP PHP

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php OOP PHP相关的知识,希望对你有一定的参考价值。

<?php
	class Person{
		private $name;
		private $email;
		private static $ageLimit = 40;

		public function __construct($name, $email){
			$this->name = $name;
			$this->email = $email;
			echo __CLASS__.' created<br>';
		}

		public function __destruct(){
			echo __CLASS__.' destroyed<br>';
		}

		public function setName($name){
			$this->name = $name;
		}

		public function getName(){
			return $this->name.'<br>';
		}

		public function setEmail($email){
			$this->email = $email;
		}

		public function getEmail(){
			return $this->email.'<br>';
		}

		public static function getAgeLimit(){
			return self::$ageLimit;
		}
	}

	#Static props and method
	//echo Person::$ageLimit;
	//echo Person::getAgeLimit();

	//$person1 = new Person('John Doe', 'jdoe@gmail.com');

	//$person1->setName('John Doe');
	//echo $person1->getName();

	//$person1->name = 'John Doe';
	//echo $person1->name;

	class Customer extends Person{
		private $balance;

		public function __construct($name, $email, $balance){
			parent::__construct($name, $email, $balance);
			$this->balance = $balance;
			echo 'A new '.__CLASS__.' has been created<br>';
		}

		public function setBalance($balance){
			$this->balance = $balance;
		}

		public function getBalance(){
			return $this->balance.'<br>';
		}
	}


	//$customer1 = new Customer('John Doe', 'jdoe@gmail.com', 300);

	//echo $customer1->getBalance();

以上是关于php OOP PHP的主要内容,如果未能解决你的问题,请参考以下文章

php OOP PHP

php php oop

php PHP OOP

PHP5中的高级OOP?

php PHP OOP覆盖受保护的属性

PHP.OOP