zoukankan      html  css  js  c++  java
  • PHP学习笔记二: 面向对象设计

    public 表示全局,类内部外部子类都可以访问;
     1 <?php
     2     
     3     class Test{
     4         public  $name='Janking',
     5                 $sex='male',
     6                 $age=23;
     7         
     8         function __construct(){
     9             echo $this->age.'<br />'.$this->name.'<br />'.$this->sex.'<br />';
    10         }
    11         
    12          function func(){
    13             echo $this->age.'<br />'.$this->name.'<br />'.$this->sex.'<br />';
    14         }
    15     }
    16 
    17 
    18 $P=new Test();
    19 echo '<br /><br />';
    20 $P->age=100;
    21 $P->name="Rainy";
    22 $P->sex="female";
    23 $P->func();
    24 ?> 
    Public
    private表示私有的,只有本类内部可以使用;
     1 <?php
     2     
     3     class Test{
     4         private  $name='Janking',
     5                 $sex='male',
     6                 $age=23;
     7         
     8         function __construct(){
     9             $this->funcOne();
    10         }
    11         
    12          function func(){
    13             echo $this->age.'<br />'.$this->name.'<br />'.$this->sex.'<br />';
    14         }
    15         
    16         private function funcOne(){
    17             echo $this->age.'<br />'.$this->name.'<br />'.$this->sex.'<br />';
    18         }
    19     }
    20 
    21 
    22 $P=new Test();
    23 echo '<br /><br />';
    24 $P->func();
    25 $P->age=100;        // Cannot access private property Test::$age 
    26 $P->name="Rainy";   // Cannot access private property Test::$name 
    27 $P->sex="female";   // Cannot access private property Test::$female
    28 $P->funcOne();      // Call to private method Test::funcOne() from context ''
    29 ?> 
    Private
    protected表示受保护的,只有本类或子类或父类中可以访问;


     和封装有关的魔术方法:

     __set():是直接设置私有成员属性值时,自动调用的方法
     __get():是直接获取私有成员属性值时,自动调用的方法
     __isset(); 是直接isset查看对象中私有属性是否存时自动调用这个方法
     __unset(); 是直接unset删除对象中私有属性时,自动调用的方法

     
  • 相关阅读:
    2.java基础语法(上)
    1.java概述
    Qt layout透明的问题
    Duilib 关于ChildLayout崩溃的问题
    关于注册表使用的几个问题
    win32接口获取ping值
    Web开发中遇到的问题
    DuiLib 窗口透明方法
    通过进程名杀死进程的方法--WIN32
    关于在Qt的MainWindow窗口中添加Layout的问题
  • 原文地址:https://www.cnblogs.com/w3develop/p/3077393.html
Copyright © 2011-2022 走看看