很多站长朋友们都不太清楚php面向对象例题,今天小编就来给大家整理php面向对象例题,希望对各位有所帮助,具体内容如下:
本文目录一览: 1、 一个很简单的PHP面向对象 问题 2、 php面向对象面试题 3、 一个关于PHP面向过程和面向对象的问题 4、 php面向对象问题 5、 PhP面向对象编程练习题 6、 用PHP面向对象写出如下图片的功能 一个很简单的PHP面向对象 问题如果不是写在类里 你这个就是一个函数
如果写在类里 你这个就是一个类里的一个方法
function_get()括号里如果没有参数就不要填 直接调用这个函数可以了
括号中间的是参数,意思就是你写这个函数的时候需要外部调入的数据才能完成
function __set($name,$value) 不一定是类里的属性 不是很熟悉对类 是外部传入的参数 比如
$a=1;$b=8;
function _set($a,$b)
{
return $a+$b;
}
$c= _set($a,$b);
这个时候$c就是9
不知道你看懂了没 呵呵
php面向对象面试题其实这个问题很简单啦,两分钟就可以请清楚啦,我在这里先说一下思想;
首先你得写一个类表示人,如:person类;
小刘、小张、MrJosnon、MrJohn等都是这个person类的一个实例;
并且人力资源部问问题这是这个person类当中的一个方法,然后每个人都可以继承自person类,然后又有各自己相关方法等;
然后你得写一个国家类,如:Country类,然后中国是这个国家的一个具体实例;
然后你得写一个城市类,如:City类;然后上海是City类的一个实例;
同样,你得写一个公司类,如:Company类,然后那两个公司又是Company类的两个实例,并且你还得写一个部门类,如:Depart类,然后人力资源部是部门的一个实例;
上面只是这个问题的中文描述,你可以参考一下;
一个关于PHP面向过程和面向对象的问题面向对象,是把一些常用的操作进行类封装起来,方便调用,需要用的地方,调用一下即可,这样,开发方便,维护也方便!修改这个封装的类,即可达到修改全站的目的!
面向过程,是在每一个地方都使用单独的代码进行操作,这样开发的时候重复累赘,维护的时候也很累,你修改了哪里,就只在哪里起作用!
比如,初学php,最基本的连接数据库和查询数据库都会这样写:
<?php
$Con = mysql_connect(.........);
mysql_query('set names utf8');
mysql_select_db(....);
$query = mysql_query( $sql );
while( $Rs = mysql_fetch_aray( $query ) ) {
echo $Rs[0];
}
等等这样,操作10次数据库,就写10次这样的代码!
而如果封装一个类,意义就不同了!
<?php
class mysql{
var $Con;
var $table;
public ConnEct( $local, $root, $pass, $base, $code){
$this -> Con = mysql_connect( $local, $root, $pass);
mysql_query('set names ' . $code);
mysql_select_db( $base );
}
public Tab( $Table) {
$this -> table = $Table;
return $this;
}
public Select(){
$rs = mysql_query('select * from ' . $this -> table)
while( $Rs = mysql_fetch_array( $rs ) {
$Rule[] = $Rs;
}
return $Rule;
}
}
把上面的代码保存成一个文件,比如是mysql.php,在需要操作数据库的地方引入这个文件,那么要查询数据库的一个表就非常方便了!
<?php
include_once 'mysql.php';
$Mysql = new mysql; //实例化一个类;
$Mysql -> ConnEct('localhost', 'root', 123456, 'table', 'utf8');//连接数据库
$Resul = $Mysql -> Tab('user') -> Select();//查询user表,并返回数组结果
print_r( $Resul ); //打印这个数组
以上个人见解,仅供参考
php面向对象问题定义了一堆的行为的编码
比如 'field','where','order','limit','offset','having','group','distinct','data'
方法如果是这些的一个方法的话,那么是去取参数1 的 这部分元素
__call('field', $arg1);
返回 $arg1->_options['field']
__call('sum', $arg1)
$arg1->get_field($method.'('.$field.') AS `count`');
__call('get', $arg1)
call_user_func_array(array($this, $matches[1].'_by'), $args);
不懂的地方可以在里面插入print_r 变量输出看看有哪是怎么回事就可以了。
PhP面向对象编程练习题header("Content-type: text/html; charset=utf-8");
class person{
public $name;
public $xb;
function _construct($xm,$sex){
$this->name=$xm;
$this->xb=$sex;
}
}
class student extends person{
var $xh;
function _construct($xm,$sex,$id){
$this->xh=$id;
parent::_construct($xm,$sex);
}
function getInfo(){
echo '姓名:'.$this->name.'<br>';
echo '性别:'.$this->xb.'<br>';
echo '学号:'.$this->xh.'<br>';
}
}
$stu1=new student;
$stu1->_construct('张三','男','20011020305');
$stu1->getInfo();
用PHP面向对象写出如下图片的功能index.php页面:
<html>
<head>
<title>图形计算器(面向对象)</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
</head>
<body>
<center>
<h1>图形(面积 周长)计算器)<h1>
<a href="index.php?action=rect">矩形</a>|| //action 是动作提交
<a href="index.php?action=triangle">三角形</a>||
<a href="index.php?action=circle">圆形</a>
<hr>
</center>
<?php
error_reporting(E_ALL ~E_NOTICE);
function __autoload($className)
{
include strtolower($className).".class.php";
}
echo new Form();
if(isset($_POST["sub"])){
echo new Result();
}
?>
</body>
</html>
form.class.php页面: //文件名习惯全小写
<?php
class Form{
private $action;
private $shape;
function __construct($action="index.php"){
$this->action = $action;
$this->shape = isset($_REQUEST["action"]) ? $_REQUEST["action"]:"rect";
}
function __toString(){
$form='<form action="'.$this->action.'" method="post">';
switch($this->shape){
case"rect":
$form.=$this->getRect();
break;
case"triangle":
$form.=$this->getTriangle();
break;
case"circle":
$form.=$this->getcircle();
break;
default:
$form.='请选择一个形状';
}
$form.='<input type="submit" name="sub" value="计算">';
$form.='</form>';
return $form;
}
private function getRect(){
$input='<b>请输入|矩形|的长和宽:</b><p>';
$input.='宽度:<input type="text" name="width" value="'.$_POST['width'].'"><br>';
$input.='高度:<input type="test" name="height" value="'.$_POST['height'].'"><br>';
$input.='<input type="hidden" name="action" value="rect">';
return $input;
}
private function getTriangle(){
$input='<b>请输入|三角形|的三边:</b><p>';
$input='<b>请输入|三角形|的三边:</b><p>';
$input.='第一边:<input type="text" name="side1" value="'.$_POST['side1'].'"><br>';
$input.='第二边:<input type="test" name="side2" value="'.$_POST['side2'].'"><br>';
$input.='第三边:<input type="test" name="side3" value="'.$_POST['side3'].'"><br>';
$input.='<input type="hidden" name="action" value="triangle">';
return $input;
}
private function getCircle(){
$input='<b>请输入|圆形|的半径:</b><p>';
$input.='半径:<input type="text" name="radius" value="'.$_POST['radius'].'"><br>';
$input.='<input type="hidden" name="action" value="circle">';
return $input;
}
}
?>
shape.class.php页面:
<?php
abstract class shape{
public $shapeName;
abstract function area();
abstract function perimeter();
protected function validate($value,$message="形状"){
if($value == "" ||!is_numeric($value)||$value < 0){
echo '<font color="red">'.$message.'必须为非负值的数>字,并且不能为空</font><br>';
return false;
} else{
return true;
}
}
}
?>
rect.class.php页面
<?php
class Rect extends Shape{
private $width=0;
private $height=0;
function __construct(){
$this->shapeName="矩形";
if($this->validate($_POST["width"],'矩形的宽度') $this->validate($_POST["height"],'矩形的高度'))
{
$this->width=$_POST["width"];
$this->height=$_POST["height"];
}else{exit;}
$this->width=$_POST["width"];
$this->height=$_POST["height"];
}
function area(){
return $this->width*$this->height;
}
function perimeter(){
return 2*($this->width+$this->height);
}
}
?>
triangle.class.php页面:
<?php
class Triangle extends Shape{
private $side1=0;
private $side2=0;
private $side3=0;
function __construct(){
$this->shapeName="三角形";
if($this->validate($_POST['side1'],'三角形的第一边')){
$this->side1=$_POST["side1"];
if($this->validate($_POST['side2'],'三角形的第一
边')){
$this->side2=$_POST["side2"];
if($this->validate($_POST['side3'],'三角形的第一
边')){
$this->side3=$_POST["side3"];
}
$this->side1=$_POST["side1"];
$this->side2=$_POST["side2"];
$this->side3=$_POST["side3"];
}
if(!$this->validateSum()){
echo '<font color="red">三角形的两边之和必须大于第三边
</font>';
exit;
}
//海伦公式
function area(){
$s=($this->side1+$this->side2+$this->side3)/2;
return sqrt( $s*($s-$this->side1)*($s-$this->side2)*($s-$this->side3));
}
function perimeter(){
return $this->side1+$this->side2+$this->side3;
}
private function validateSum(){
$condition1=($this->side1+$this->side2) > $this->side3;
$condition2=($this->side1+$this->side3) > $this->side2;
$condition3=($this->side2+$this->side3) > $this->side1;
if($condition1 $$ $condition2 $$ $condition3){
return true;
} else{return false;}
}
}
?>
circle.class.php界面:
<?php
class Circle extends Shape{
private $radius=0;
function __construct(){
$this->shapeName="圆形";
if($this->validate($_POST['radius'],'圆的半径')){
$this->radius.$_POST["radius"];
}else{exit;}
$this->radius=$_POST["radius"];
}
function area(){
return pi()*$this->radius*$this->radius;
}
function perimeter(){
return 2*pi()*$this->radius;
}
}
?>
result.class.php界面:
<?php
class Result{
private $shape;
function __construct(){
switch($_POST['action']){
case 'rect':
$this->shape=new Rect();
break;
case 'triangle':
$this->shape=new Triangle();
break;
case 'circle':
$this->shape=new Circle();
break;//没有break会导致default的执行
default:
$this->shape=false;
}
}
function __toString(){
if($this->shape){
$result=$this->shape->shapeName.'的周长'.$this->shape->perimeter().'<br>';
$result.=$this->shape->shapeName.'的面积'.$this->shape->area().'<br>';
return $result; }
else{
return'没有这个形状';
}
}
}
?>
关于php面向对象例题的介绍到此就结束了,不知道本篇文章是否对您有帮助呢?如果你还想了解更多此类信息,记得收藏关注本站,我们会不定期更新哦。
查看更多关于php面向对象例题 php面向对象实例的详细内容...