php中面向对象

前端之家收集整理的这篇文章主要介绍了php中面向对象前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<h2 style="border-color: #59c3f9; margin: 8px 0px 0px; padding: 0px; text-align: justify; color: #59c3f9; line-height: 28px; font-size: 16px; font-weight: bold; border-bottom-width: 1.5px; border-bottom-style: solid; min-height: 32px; max-width: 100%"><span class="autonum" style="margin: 0px 8px 0px 0px; padding: 4px 10px; color: #ffffff; line-height: 20px; float: left; display: block; max-width: 100%">一<span style="font-size: 16px"><strong class="135brush" style="border-color: #59c3f9; color: inherit">面向对象

属性(特征)和方法(行为)的一系列个体的集合,类是一个抽象的概念。

属性值的个体,称为对象。对象是一个具体的个体。

属性,但是不能有具体的值,所以类是抽象的。属性赋值后,产生具体的个体,所有对象是具体的。

<h2 style="border-color: #59c3f9; margin: 8px 0px 0px; padding: 0px; text-align: justify; color: #59c3f9; line-height: 28px; font-size: 16px; font-weight: bold; border-bottom-width: 1.5px; border-bottom-style: solid; min-height: 32px; max-width: 100%"><span class="autonum" style="margin: 0px 8px 0px 0px; padding: 4px 10px; color: #ffffff; line-height: 20px; float: left; display: block; max-width: 100%">二<span style="font-size: 16px"><strong class="135brush" style="border-color: #59c3f9; color: inherit">类的声明与实例化

属性[=默认值];方法(){}

属性必须要带访问修饰符,方法可以不带访问修饰符。

属性方法调用调用属性方法 $属性名;调用属性时,属性名不能带$符号调用属性方法 $属性名;

<h2 style="border-color: #59c3f9; margin: 8px 0px 0px; padding: 0px; text-align: justify; color: #59c3f9; line-height: 28px; font-size: 16px; font-weight: bold; border-bottom-width: 1.5px; border-bottom-style: solid; min-height: 32px; max-width: 100%"><span class="autonum" style="margin: 0px 8px 0px 0px; padding: 4px 10px; color: #ffffff; line-height: 20px; float: left; display: block; max-width: 100%">三<span style="font-size: 16px"><strong class="135brush" style="border-color: #59c3f9; color: inherit">构造函数

函数?函数是类中的一个特殊函数,当我们使用new关键字实例化对象时,相当于调用了类的构造函数

函数有什么作用?自动调用,用于给对象的属性赋初值!

函数的写法:函数名,必须与类同名 name = $name;方法__construct name = $name;函数注意事项:函数名必须与类同名!!!!函数,则系统默认会有一个空参构造,因此可以使用new Person();函数,则将不会再有空参构造,也就是不能直接使用new Person();函数的要求!!!!函数同时存在,将使用__construct。

函数:__destruct():函数在对象被销毁释放之前自动调用函数不能带有任何的参数;函数常用于对象使用完以后,释放资源,关闭资源等。

方法:PHP中,给我们提供一系列用__开头的函数,这些函数无需自己手动调用自动调用,这类函数称为魔术称为魔术函数自动调用自动调用

方法之外,自定义函数方法不能使用__开头。

功能比较复杂的类,我们会单独的写到一个类文件中。

文件的命名,同一小写,使用"类名小写.class.PHP"的方式命名。文件中使用这个类时,可以使用include导入这个".class.PHP"文件

属性和方法进行私有化处理,以实现访问控制。

属性后,需要提供对应的方法,让用户通过我们提供的方法处理属性

功能,不关心功能实现的细节!(封装方法用户的数据进行控制,防止设置不合法数据,控制返回给用户的数据(属性封装+set/get方法

方法的封装方法,而不像对外部提供使用,那么,这样的方法我们可以使用private进行私有化处理。

SyntaxHighlighter PHP" border="0" cellspacing="0" cellpadding="0">PHP plain">1 PHP keyword">private PHP keyword">function PHP plain">formatName(){} formatName();4 }

</td>

</tr></table>

属性的封装+set/get方法属性的设置以及读取,可以将属性进行私有化处理,并要求用户通过我们提供的set/get方法进行设置

SyntaxHighlighter PHP" border="0" cellspacing="0" cellpadding="0">PHP plain">1 PHP keyword">private PHP variable">$agePHP plain">;2 age=$age;5 }6 //get方法7 function getAge(){8 return $this->age;9 }

</td>

</tr></table>

getAge();setAge(12);

属性的封装+魔术方法

SyntaxHighlighter PHP" border="0" cellspacing="0" cellpadding="0">   __set(,->= }

</td>

</tr></table>

age; //访问对象私有属性时,自动调用__get()魔术方法,并且将访问的属性名传给__get()方法age=12; //设置对象私有属性时,自动调用__set()魔术方法,并且将设置的属性名以及属性值传给__set()方法

方法中,可以使用分支结构,判断$key的不同,进行不同操作。

方法:属性赋值时自动调用调用时给方法传递两个参数:需要设置的属性名,属性值。属性时自动调用调用时给方法传递一个参数,需要读取的属性名;函数检测私有属性时,自动调用>>类外部使用isset();检测私有属性,默认是检测不到的。false>>所以,我们可以使用__isset();函数,在自动调用时,返回内部检测结果。

SyntaxHighlighter PHP" border="0" cellspacing="0" cellpadding="0">PHP plain">1 PHP keyword">function PHP plain">__isset(PHP variable">$keyPHP plain">){2 PHP keyword">return PHP plain">isset(PHP variable">$this PHP variable">$keyPHP plain">);3 }

</td>

</tr></table>

私有属性);检测时,将自动调用上述__isset()返回的结果!

函数删除私有属性时,自动调用 私有属性);删除属性时,自动属性名传给__unset(),并交由这个魔术方法处理。

<div style="border-width: 1px 1px 1px 5px; border-style: solid; border-color: #a4bed4; margin: 10px 0px; padding: 10px">
<h4 class="135brush" style="border-color: #a4bed4; margin: 5px 0px; color: #8094a4; font-size: 18px; font-weight: bold"><span style="font-size: 18pt">继承的基础知识:

父类;

父类的非私有属性父类后,相当于将父类属性方法copy到子类,可以直接使用$this调用PHP只能单继承,不支持一个类继承多个类。但是一个类进行多层继承。属性和方法

方法覆盖(方法重写)父类父类已有方法

方法覆盖。覆盖之后,子类调用方法,将调用子类自己的方法方法覆盖,子类也可以具有与父类同名的属性,进行属性覆盖。

父类方法,如何在子类中调用父类同名方法

方法名();父类时,需在子类的构造中的第一步,首先调用父类构造进行复制。

SyntaxHighlighter PHP" border="0" cellspacing="0" cellpadding="0">PHP plain">1 PHP keyword">function PHP plain">__construct(PHP variable">$namePHP plain">,PHP variable">$sexPHP plain">,PHP variable">$schoolPHP plain">){2 partent::__construct(PHP variable">$namePHP plain">,PHP variable">$sexPHP plain">);3 PHP variable">$this school = PHP variable">$schoolPHP plain">;4 }

</td>

</tr></table>

PHP关键字

方法,此方法为最终方法,不能被重写!属性。

属性和方法,分别称为静态属性和静态方法,也叫类属性,类方法属性,静态方法,只能使用类名直接调用属性","类名::静态方法()"属性和方法,在类装载时就会声明,先于对象产生。方法中,不能调用非静态属性方法方法,可以调用静态属性方法属性和方法在类装载时已经产生,而非静态的属性方法,此时还没有实例化诞生)

SyntaxHighlighter PHP" border="0" cellspacing="0" cellpadding="0">PHP plain">1 PHP keyword">class PHP plain">Person{2 PHP keyword">static PHP variable">$sex PHP plain">= PHP string">"nan"PHP plain">;3 PHP keyword">function PHP plain">say(){4 PHP functions">echo PHP plain">self::PHP variable">$sexPHP plain">;5 }6 }

</td>

</tr></table>

属性是共享的,也就是new出很多对象,也是共用一个属性

函数!必须使用const关键字。调用时与static一样,使用类名调用Person::常量。

包括爹辈,爷爷辈,太爷爷辈……)

SyntaxHighlighter PHP" border="0" cellspacing="0" cellpadding="0">PHP variable">$zhangsan PHP keyword">instanceof PHP plain">Person;

</td>

</tr></table>

<div style="border-width: 1px 1px 1px 5px; border-style: solid; border-color: #a4bed4; margin: 10px 0px; padding: 10px">
<h4 class="135brush" style="border-color: #a4bed4; margin: 5px 0px; color: #8094a4; font-size: 18px; font-weight: bold">
【小总结】几种特殊操作符

声明数组时,关联键与值["key"=>"value"] 对象($this new出的对象)调用成员属性,成员方法调用父类中的同名方法:parent::say();调用类中的静态属性,静态方法,以及常量。

函数私有化,不允许使用new关键字创建对象。获取对象的方法,在方法中判断对象是否为空。属性以及获取对象的方法必须都是静态的。方法。

方法

方法小总结(12个)

<h2 style="border-color: #59c3f9; margin: 8px 0px 0px; padding: 0px; text-align: justify; color: #59c3f9; line-height: 28px; font-size: 16px; font-weight: bold; border-bottom-width: 1.5px; border-bottom-style: solid; min-height: 32px; max-width: 100%"><span class="autonum" style="margin: 0px 8px 0px 0px; padding: 4px 10px; color: #ffffff; line-height: 20px; float: left; display: block; max-width: 100%">一<span style="font-size: 16px"><strong class="135brush" style="border-color: #59c3f9; color: inherit">clone与__clone

自动调用clone函数函数,类似于克隆时使用的构造函数,可以给新克隆对象赋初值。函数里面的$this指的是新克隆的对象不支持。输出语句,直接打印对象时调用echo $zhangsan;函数返回的字符串;

SyntaxHighlighter PHP" border="0" cellspacing="0" cellpadding="0">PHP plain">1 PHP keyword">function PHP plain">__toString(){2 PHP keyword">return PHP string">"haha"PHP plain">;3 }4 PHP functions">echo PHP variable">$zhangsanPHP plain">; PHP comments">//结果为:haha

</td>

</tr></table>

调用类中未定义或未公开的方法时,会自动执行__call()方法自动执行时,会给__call()方法传递两个参数;调用的方法调用方法的参数列表。

<h2 style="border-color: #59c3f9; margin: 8px 0px 0px; padding: 0px; text-align: justify; color: #59c3f9; line-height: 28px; font-size: 16px; font-weight: bold; border-bottom-width: 1.5px; border-bottom-style: solid; min-height: 32px; max-width: 100%"><span class="autonum" style="margin: 0px 8px 0px 0px; padding: 4px 10px; color: #ffffff; line-height: 20px; float: left; display: block; max-width: 100%">二__antoload()

方法;自动调用这个魔术方法调用时,会自动给__autoload()传递一个参数:实例化的类名方法实现自动加载文件功能

SyntaxHighlighter PHP" border="0" cellspacing="0" cellpadding="0">PHP plain">1 PHP keyword">function PHP plain">__autoload(PHP variable">$classNamePHP plain">){2 PHP keyword">include    PHP string">"class/"PHP plain">.PHP functions">strtolowerPHP plain">(PHP variable">$classNamePHP plain">).PHP string">".class.PHP"PHP plain">;3 }4 PHP variable">$zhangsanPHP plain">=PHP keyword">new PHP plain">Person();PHP comments">//本文件内没有Person类,会自动执行__autoload()加载person.class.PHP文件

</td>

</tr></table>

<h2 style="border-color: #59c3f9; margin: 8px 0px 0px; padding: 0px; text-align: justify; color: #59c3f9; line-height: 28px; font-size: 16px; font-weight: bold; border-bottom-width: 1.5px; border-bottom-style: solid; min-height: 32px; max-width: 100%"><span class="autonum" style="margin: 0px 8px 0px 0px; padding: 4px 10px; color: #ffffff; line-height: 20px; float: left; display: block; max-width: 100%">三<span style="font-size: 16px"><strong class="135brush" style="border-color: #59c3f9; color: inherit">面向对象串行化与反串行化(序列化与反序列化)

文件或数据库中持久保存的时候方法:自动执行__sleep()函数函数要求返回一个数组,数组中的值,就是可以串行化的属性;不在数组中的属性,不能被串行化;属性可以串行化。方法自动调用__wakeup()方法自动调用时,用于给反串行化产生的新对象属性,进行重新赋值。 name = "李四"

加上数据类型,用于约束此变量只能存放对应的数据类型。PHP中,只能实现数组和对象的类型约束)PHP中,类型约束,只能发生在函数的形参中。

SyntaxHighlighter PHP" border="0" cellspacing="0" cellpadding="0">PHP plain">1 PHP keyword">class PHP plain">Person{}2 PHP keyword">class PHP plain">Student PHP keyword">extends PHP plain">Person{}3 PHP keyword">function PHP plain">func(Person PHP variable">$pPHP plain">){ name;6 }

</td>

</tr></table>

父类

方法小总结

函数,new一个对象时,自动调用函数,当一个对象被销毁前,自动调用属性属性赋值时,自动调用。传递需要设置的属性名和属性值; 属性名); 属性名);自动调用。返回想要在打印对象时,显示内容;返回必须是字符串;调用一个类中未定义或未公开的方法时,自动调用。传递被调用函数名,和参数列表数组;自动调用。作用是为新克隆的对象进行初始化赋值;自动调用。返回一个数组,数组中的值就是可以序列化的属性自动调用。为反序列化新产生的对象,进行初始化赋值;函数。当实例化一个未声明的类时,自动调用。传递实例化的类名,可以使用类名自动加载对应的类文件

方法

方法?方法体{}的方法,必须使用abstract关键字修饰。这样的方法,我们称为抽象方法方法

方法;方法的类必须是抽象类,抽象类并不一定必须包含抽象方法方法,抽象方法没有方法体,实例化调用没有意义)

父类的所有抽象方法,除非,子类也是抽象类。

方法没有方法体,所以不能实例化)方法。

方法组合。

方法,必须都是抽象方法方法不需要也不能使用abstract修饰。

属性,只能使用常量!!!

方法!

①声明方式上,接口使用interface关键字,抽象类使用abstract class。②实现/继承方式上,一个类使用extends继承抽象类,使用implements实现接口。③抽象类只能单继承,接口可以多实现。(接口extends接口)、多实现(类implements接口)④抽象类中可以有非抽象方法,接口中只能有抽象方法,不能有费抽象方法。抽象类中的抽象方法必须使用abstract关键字修饰,接口中抽象方法不能带修饰词。⑤抽象类是个类,可以有属性、变量;接口中只能有常量。

方法,在多个子类中,表现出不同的功能,我们称这种行为为多态。

父类;父类方法父类引用指向子类对象

猜你在找的PHP相关文章