• PHP基础OOP(一)


    <!DOCTYPE html>
    <html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
        <title></title>
        <link rel="stylesheet" type="text/css" href="Basedemo/DemoPage.css"></head>
    
    <body>
        <div id="main">
            <div id="wrapper">
    
                <h3>类和对象的创建</h3>
                <div id ="content">
                    <?php
                    //
                    //================PHP的面向对象基础===============
                    //
                    //
                    //PHP的面向对象是在PHP4.0时引入的设计
                    //对象:使用新的数据类型管理相同事物的变量
                    //类的命名规范:类名.class.php(Animal.class.php)
                    //基本类的定义 class 类名{属性;构造方法;成员方法;析构方法};
                    //
                    //====类的引用或包含:
                    //require 'prepend.php';
                    //require ('somefile.txt');
                    //require_once'Animal.class.php'; 
                    //include(Animal.class.php)
                    //include_once("a.php");
                    //区别:
                    //加once的,如果重复引用,则只引一次
                    //include和require的区别在于,在处理失败时的提示
                    //include处理失败提示警告,后面的代码会执行
                    //require处理失败提示致命错误,后面的代码不执行
                    //
                    //====类成员的访问
                    //PHP类的实例化:$cat = new Animal(); 或者 $cat = new Animal;带括号或不带都可以
                    //对象属性的访问和赋值: $cat ->name="小白";
                    //对象方法的访问:$cat->Say();
                    //
                    //====内存数据分布:静态区、常量区、代码区、堆区、栈区
                    //堆区:存放对象
                    //栈区:存放基本数据类型和引用类型的引用地址
                    //
                    //====PHP的值传递与引用传递
                    //给函数传递一个对象,是引用传递,给函数传一个基本数据类型(整数、小数、布尔、字符),是值传递
                    //【基本数据类型中的字符串类型与C#不同,因为他们在传递过程中都是值传递】
                    //如果希望是引用传递则在变量前加&符:function fun(&变量,&变量,...){}
                    //
                    //
                    
                    //定义一个动物类Animal:
                    class Animal
                    {
                        public $name;
                        public $age;
                        public $color;
    
                        public function Say()
                        {
                            echo "这是一只".$this->color."的".$this->name."今年已经".$this->age."岁了<br/>";
                        }
                    }
    
                    //实例化一个对象
                    $cat = new Animal();
    
                    $cat->name = "小猫";
                    $cat->age=1;
                    $cat->color="白色";
                    $cat->Say();
                    //echo "这是一只".$cat->color."的".$cat->name."今年已经".$cat->age."岁了<br/>";
                    //output: 这是一只白色的小猫今年已经1岁了
    
                    //实例化一个对象
                    $dog = new Animal();
    
                    $dog->name = "小狗";
                    $dog->age=2;
                    $dog->color="黑色";
                    $dog->Say();
                    //echo "这是一只".$dog->color."的".$dog->name."今年已经".$dog->age."岁了<br/>";
                    //output: 这是一只黑色的小狗今年已经2岁了
                    
    
                    ?>
                </div>
    
                <h3>函数的细节</h3>
                <div id ="content">
                    <?php
                        //参数列表:参数可以是任意类型
                        //方法可以有返回值,也可以没有返回值
                        function FindMax($arr)
                        {
                            $maxVal=$arr[0];
                            $maxIndex=0;
                            for ($i=1; $i <count($arr) ; $i++) { 
                                if($maxVal<$arr[$i])
                                {
                                    $maxVal = $arr[$i];
                                    $maxIndex = $i;
                                }
                            }
                            return $maxVal;
                        }
    
                        $arr1 = array(2,8,9,11,1,5);
                        $result = FindMax($arr1);
                        var_dump($arr1);
                        echo "<br/>最大的数是:";
                        echo $result;
    
                    ?>
                </div>
    
                <h3>构造函数 与 析构函数</h3>
                <div id ="content">
                    <?php
                        /**
                        * 构造函数:function __construct(){}
                        * 1.类实例化时赋值,完成对类的对象初始化
                        * 2.每个类有一个默认的构造函数,自定义的构造函数会覆盖默认的构造函数
                        * 3.一个类只能有一个构造函数,不能重载
                        * 4.构造函数没有返回值,创建对象时自动调用
                        * 5.修饰符 默认为public
                        * 6.PHP4.0 的中的形式:public function Person(){}
                        * 7.PHP5新增的写法:public function __construct(){},PHP4中的写法依然有效
                        */
                        class Person
                        {
                            public $Name;
                            public $Age;
                            public $Sex;
                            
                            //构造函数
                            public function __construct($Name,$Age,$Sex)
                            {
                                echo "构造函数执行了<br/>";
                                $this->Name = $Name;
                                $this->Age = $Age;
                                $this->Sex=$Sex;
                            }
    
                            //自定义函数
                            public function Say()
                            {
                                echo "姓名:".$this->Name.", 性别:".$this->Sex.", 年龄:".$this->Age."<br/>";
                            }
    
                            /*
                            *析构函数:function __destruct(){}
                            *用于释放对象资源(如:数据库链接、图片文件资源等)
                            *会自动调用,释放对象资源,并不销毁对象本身
                            *一个类只能有一个析构方法,没有参数
                            *对象的释放顺序,在栈中为先进后出,即最先创建的对象最后销毁
                            *垃圾回收:当对象没有任何引用指向时,会被自动回收(析构函数会立即调用)
                            */
                            function  __destruct()
                            {
                                echo "析构函数执行了,销毁对象: ".$this->Name."<br/>";
                            }
    
                        }
    
                        //实例化一个人类:
                        $p1 = new Person("张三","22","男");
                        $p2 = new Person("小美",19,"女");
                        $p1 ->Say();
                        $p2 ->Say();
                    ?>
                </div>
    
            </div>
        </div>
    
    
    
    
    
        <div id="footer">
            <div class="tri"></div>
            <h1>Write some things for your project!</h1>
        </div>
    </body>
    </html>
  • 相关阅读:
    学习dubbo
    【Spring】SpringMVC配置文件
    Mac下git配置
    【Spring】入门HelloWorld
    【MySql】启动/停止
    Javaweb 编解码流程
    TensorFlow学习笔记1
    Nginx 代理配置
    【转】RPC介绍
    【dubbo】dubbo控制台搭建
  • 原文地址:https://www.cnblogs.com/lt-style/p/3491658.html
Copyright © 2020-2023  润新知