Simple example of object-oriented programming in php

WBOY
Release: 2016-07-25 09:00:59
Original
1247 people have browsed it
Let me introduce you to a simple example of PHP object-oriented programming for your reference.

Required implementation: write a function (developed in PHP object-oriented way), input an integer from the web page and print out the corresponding pyramid. It is involved in many PHP tutorials. Today I will show it to you with a simple example.

Code example: 1. Display page pview.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>打印金字塔_程序员之家_bbs.it-home.org</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
</head>
<body>
    <form action="print.php" method="post">
        请输入一个数:<input type="text" name="one" />
        <input type="submit" value="提交" />
    </form>
</body>
</html>
Copy after login

2. Print page:

<?php
    //引入class.php 文件
    require_once 'class.php';
    //接收传过来的值
    $one=$_REQUEST['one'];
    
    //创建一个对象
    $p = new Jzit;
    //调用成员方法
    $p->printd($one);
?>
Copy after login

3. Class file: class.php

<?php
    //编写一个函数(以面向对象的方式开发),从网页输入一个整数打印出对应的金子塔
    //定义一个类 用来打印金字塔 by bbs.it-home.org
    class Jzit{
    
        //定义接收一个值来打印金字塔成员方法
        public function printd($n){
            
            //先定义层数
            for($i=1;$i<=$n;$i++){
                //打印空格
                for($k=1;$k<=$n-$i;$k++){
                    
                    echo " ";
                }
                //打印*号
                for($j=1;$j<=2*$i-1;$j++){
                    
                    echo "*";
                }
                //换行,打印下一行
            echo "<br />";
            }
        }
    }
?>
Copy after login


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template