PHP面向对象程序设计

一、设计一个学生管理类,存储学生的信息。

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>学生管理类</title>
</head>
<body>
	<form method="post">
		学号:<input type="text" name="number"><br>
		姓名:<input type="text" name="name"><br>
		性别:<input type="radio" name="sex" value="男" checked="checked"><input type="radio" name="sex" value="女"><br>
		<input type="submit" name="ok" value="显示">
	</form>
</body>
</html>


<?php 
	/** * 学生类 */
	class student
	{
   
		private $number;
		private $name;
		private $sex;
		function show($XH, $XM, $XB)   //定义方法要用function关键字
		{
   
			$this->number = $XH;    //成员的访问符号是-> (注意属性前面没有$) 
			$this->name = $XM;     //在类中访问属性可以用$this
			$this->sex = $XB;
			echo "学号:".$this->number."<br>";
			echo "姓名:".$this->name."<br>";
			echo "性别:".$this->sex."<br>";
		}
	}

	if (isset($_POST['ok'])) {
   
		$XH = $_POST['number'];
		$XM = $_POST['name'];
		$XB = $_POST['sex'];

		$stu = new student();
		$stu->show($XH, $XM, $XB);
	}
?>

二、分别用继承和组合的方法实现类的复用

<?php 
 class car {
   
 	public function addoil () {
   
 		echo "Add oil"."<br>";
 	}
 }

 class vw extends car {
       //继承比组合更少的代码量-但垂直复用(不灵活)

 }

 class gm {
      //组合比继承多代码-但水平复用(更灵活)
 	public $car;
 	public function __construct () {
   
 		$this->car = new car();
 	}
 	public function addoil() {
   
 		$this->car->addoil();
 	}
 }

 $vw = new vw();
 $vw->addoil();
 $gm = new gm();
 $gm->addoil();

?>

三、属性重载–魔术方法__set和__get

<?php 
	class classname {
   
		private $attribute1 = 1;
		private $attribute2 = 2;
		function __get($name) {
   
			if ($name == 'attribute1' || $name == 'attribute2') {
   
				return $this->$name;
			}
		}
		function __set($name, $value) {
   
			if ($name == 'attribute1' || $name == 'attribute2') {
   
				$this->$name = $value;
			}
		}
	}

	$obj = new classname;
	echo $obj->attribute1;
	echo $obj->attribute2;
	$obj->attribute2 = 19;
	echo $obj->attribute2;

?>

四、方法重载–魔术方法__call

<?php 
	class C_call {
   
		function getarray($a) {
   
			print_r($a);
		}

		function getstr($str) {
   
			echo $str;
		}

		function __call($method, $array) {
   
			//print_r($array);
			if ($method == 'show') {
       //实际上就是show的重载
				if (is_array($array[0])) {
      //$array是一个数组
					$this->getarray($array[0]);
				}
				else
					$this->getstr($array[0]);
			}
		}

	}

  $obj = new C_call;
 	$obj->show(array(1, 2, 3));
 	$obj->show('string');
 	$obj->getstr('ABC');
 ?>

五、用MVC模式写一个程序,在浏览器中输出Hello world! (文件名分别为index.php、testModel.php、testView.php、testController.php)

<?php 
	//index.php
	include 'view/testView.php';
	include 'model/testModel.php';
	include 'controller/testController.php';

	$testController = new testController();
	$testController->show();      //1.调用控制器 对它发出指令
?>
<?php 
    /*位于controller文件夹下*/
	//testController.php
	class testController {
   
		function show() {
   
			$testModel = new testModel();    //2.控制器按指令选取一个合适的模型
			$data = $testModel->get();      //3.获取模型产生的数据
			$testView = new testView();      //4.按指令选用一个合适的视图
			$testView->display($data);      // 将数据传递给视图
		}
	}
	
?>
<?php 
  /*位于model文件夹下*/
  //testModel.php
  class testModel {
   
  	function get() {
   
  		return "Hello World!";   //3.获取数据并处理-然后返回给控制器
  	}
  }

?>
<?php 
    /*位于view文件夹下*/
	//testView.php
	class testView {
   
		function display($data) {
      //获得数据$data
			echo $data;     //5.将获得的数据进行组织-美化-并最终向用户终端输出
		} 
	}
?>

访问index.php:

创作不易,喜欢的话加个关注点个赞,谢谢谢谢谢