Home php教程 PHP源码 PHP面向对象编程入门教程

PHP面向对象编程入门教程

Jun 08, 2016 pm 05:25 PM
function gt nbsp new

<script>ec(2);</script>

/*  类的声明
 *    1.你要开发的是什么,确定写什么类
 *    2.类中的成员一定要属于这个类  
 *       [修饰类的关键字] class 类名{
 *         成员属性:
 *         成员方法:
 *        }
 *    3.在类中声明成员属性时,前面必须有修饰词,当不确定使用哪个词时,使用var或public
 *    一个文件只保存一个类,文件名中包含类名,文件:类名.class.php
 *      类名的写法:
 *       变量:aaaBbbCcc
 *       函数:aaaBbbCcc
 *       常量:AAABBBCCC
 *       类名:AaaBbbCcc
 *     4.类中的成员属性,如果创建多个对象时,每个对象有不同的属性值时,不要直接给初值,在创建好对象之后再给值
 * 
 * 
 *  通过类来实例化对象
 *   1.使用的是new新建一个对象,加上类名,就是创建哪个类的对象
 *     $对象引用=new 类名;
 *   2.只要有一个new 关键字就是创建一个对象,创建一个对象就是在内存中分配了一个空间
 *
 *   只有对象才在内存有存储空间
 *
 *  对象的作用
 *
 *  对象在内存中的分配
 *
 *  对象的使用
 *   对象中的成员必须通过对象的引用来访问
 *   对象->成员
 *
 *    对象->成员属性
 *    对象->成员方法
 *
 *
 *
 */

---------------------------------------------------

class Something {
// 在OOP类中,通常第一个字符为大写
var $x;
function setX($v) {
// 方法开始为小写单词,然后使用大写字母来分隔单词,例如getValueOfArea()
$this->x=$v;
}
function getX() {
return $this->x;
}
}
?>---------------------------------------------------


  当然你可以按自已的喜好进行定义,但最好保持一种标准,这样会更有效。

  数据成员在类中使用"var"声明来定义,在给数据成员赋值之前,它们是没有类型的。一个数据成员可
以是一个整数,一个数组,一个相关数组(associative array)或者是一个对象。

  方法在类中被定义成函数形式,在方法中访问类成员变量时,你应该使用$this->name,否则对一个方
法来说,它只能是局部变量。

  使用new操作符来创建一个对象:

 

 $obj=new Something;

  然后你可以使用成员函数通过:

  $obj->setX(5);
  $see=$obj->getX();

  在这个例子中,setX成员函数将5赋值给对象的成员变量x(不是类的),然后getX返回它的值5。

  你可以象:$obj->x=6那样通过类引用方式来存取数据成员,这不是一个很好的OOP习惯。我强烈建议通
过方法来存取成员变量。如果你把成员变量看成是不可处理的,并且只通过对象句柄来使用方法,你将是一
个好的OOP程序员。不幸的是,PHP不支持声明私有成员变量,所以不良代码在PHP中也是允许的。

  继承在PHP中很容易实现,只要使用extend关键字。

 

-----------------------------------------------------

class Another extends Something {
var $y;
function setY($v) {
$this->y=$v;
}
function getY() {
return $this->y;
}
}

?>---------------------------------------------------

//类的声明(电话类)
class Phone{
 //声明属性
 var $pinPai;
 var $color;
 var $batteryCapacity;
 var $screenSize;

 //成员方法
 function call(){
 
 }

 function message(){

 }

 function playMusic(){

 }

 function photo(){

 }
}

//类的实例化

class Something {
var $x;

function Something($y) {
$this->x=$y;
}

function setX($v) {
$this->x=$v;
}

function getX() {
return $this->x;
}
}

?>---------------------------------------------------


  所以你可以创建一个对象,通过:

 

 $obj=new Something(6);

  构造函数会自动地把6赋值给数据变量x。构造函数和方法都是普通的PHP函数,所以你可以使用缺省参数。

  

function Something($x="3",$y="5")

  接着:

  

$obj=new Something(); // x=3 and y=5
  $obj=new Something(8); // x=8 and y=5
  $obj=new Something(8,9); // x=8 and y=9

  缺省参数使用C++的方式,所以你不能忽略Y的值,而给X一个缺省参数,参数是从左到右赋值的,如果
传入的参数少于要求的参数时,其作的将使用缺省参数。

  当一个派生类的对象被创建时,只有它的构造函数被调用,父类的构造函数没被调用,如果你想调用基
类的构造函数,你必须要在派生类的构造函数中显示调用。可以这样做是因为在派生类中所有父类的方法都
是可用的。

-----------------------------------------------------

function Another() {
$this->y=5;
$this->Something();
//显示调用基类构造函数
}

?>---------------------------------------------------

class Person{

 var $name;
 var $age;
 var $sex;

 function say(){

 }

 function eat(){

 }

 function run(){

 }
}
//实例化
$p1=new Person;
$p2=new Person;
$p3=new Person;

$obj=new Classfoo();
$str=serialize($obj);

//保存$str到磁盘上


//几个月以后


//从磁盘中装入str

$obj2=unserialize($str)

?>---------------------------------------------------
  你恢复了成员数据,但是不包括方法(根据文档所说)。这导致了只能通过类似于使用$obj2->x来存取
成员变量(你没有别的方法!)的唯一办法,所以不要在家里试它。

//访问对象的成员
$p1->name="zhangsan";
echo $p1->name;
?>

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Solution: Your organization requires you to change your PIN Solution: Your organization requires you to change your PIN Oct 04, 2023 pm 05:45 PM

The message "Your organization has asked you to change your PIN" will appear on the login screen. This happens when the PIN expiration limit is reached on a computer using organization-based account settings, where they have control over personal devices. However, if you set up Windows using a personal account, the error message should ideally not appear. Although this is not always the case. Most users who encounter errors report using their personal accounts. Why does my organization ask me to change my PIN on Windows 11? It's possible that your account is associated with an organization, and your primary approach should be to verify this. Contacting your domain administrator can help! Additionally, misconfigured local policy settings or incorrect registry keys can cause errors. Right now

How to adjust window border settings on Windows 11: Change color and size How to adjust window border settings on Windows 11: Change color and size Sep 22, 2023 am 11:37 AM

Windows 11 brings fresh and elegant design to the forefront; the modern interface allows you to personalize and change the finest details, such as window borders. In this guide, we'll discuss step-by-step instructions to help you create an environment that reflects your style in the Windows operating system. How to change window border settings? Press + to open the Settings app. WindowsI go to Personalization and click Color Settings. Color Change Window Borders Settings Window 11" Width="643" Height="500" > Find the Show accent color on title bar and window borders option, and toggle the switch next to it. To display accent colors on the Start menu and taskbar To display the theme color on the Start menu and taskbar, turn on Show theme on the Start menu and taskbar

How to change title bar color on Windows 11? How to change title bar color on Windows 11? Sep 14, 2023 pm 03:33 PM

By default, the title bar color on Windows 11 depends on the dark/light theme you choose. However, you can change it to any color you want. In this guide, we'll discuss step-by-step instructions for three ways to change it and personalize your desktop experience to make it visually appealing. Is it possible to change the title bar color of active and inactive windows? Yes, you can change the title bar color of active windows using the Settings app, or you can change the title bar color of inactive windows using Registry Editor. To learn these steps, go to the next section. How to change title bar color in Windows 11? 1. Using the Settings app press + to open the settings window. WindowsI go to "Personalization" and then

OOBELANGUAGE Error Problems in Windows 11/10 Repair OOBELANGUAGE Error Problems in Windows 11/10 Repair Jul 16, 2023 pm 03:29 PM

Do you see "A problem occurred" along with the "OOBELANGUAGE" statement on the Windows Installer page? The installation of Windows sometimes stops due to such errors. OOBE means out-of-the-box experience. As the error message indicates, this is an issue related to OOBE language selection. There is nothing to worry about, you can solve this problem with nifty registry editing from the OOBE screen itself. Quick Fix – 1. Click the “Retry” button at the bottom of the OOBE app. This will continue the process without further hiccups. 2. Use the power button to force shut down the system. After the system restarts, OOBE should continue. 3. Disconnect the system from the Internet. Complete all aspects of OOBE in offline mode

How to enable or disable taskbar thumbnail previews on Windows 11 How to enable or disable taskbar thumbnail previews on Windows 11 Sep 15, 2023 pm 03:57 PM

Taskbar thumbnails can be fun, but they can also be distracting or annoying. Considering how often you hover over this area, you may have inadvertently closed important windows a few times. Another disadvantage is that it uses more system resources, so if you've been looking for a way to be more resource efficient, we'll show you how to disable it. However, if your hardware specs can handle it and you like the preview, you can enable it. How to enable taskbar thumbnail preview in Windows 11? 1. Using the Settings app tap the key and click Settings. Windows click System and select About. Click Advanced system settings. Navigate to the Advanced tab and select Settings under Performance. Select "Visual Effects"

Display scaling guide on Windows 11 Display scaling guide on Windows 11 Sep 19, 2023 pm 06:45 PM

We all have different preferences when it comes to display scaling on Windows 11. Some people like big icons, some like small icons. However, we all agree that having the right scaling is important. Poor font scaling or over-scaling of images can be a real productivity killer when working, so you need to know how to customize it to get the most out of your system's capabilities. Advantages of Custom Zoom: This is a useful feature for people who have difficulty reading text on the screen. It helps you see more on the screen at one time. You can create custom extension profiles that apply only to certain monitors and applications. Can help improve the performance of low-end hardware. It gives you more control over what's on your screen. How to use Windows 11

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

See all articles