PHP面向对象编程-简介

时间:2020-02-23 14:42:01  来源:igfitidea点击:

在本教程中,我们将学习有关PHP中的面向对象编程的知识。

什么是面向对象编程?

面向对象编程(OOP)是一种基于具有属性的对象和在该属性上工作的方法的编程范例。

以下是一些OOP语言:C ++,Java,PHP,C#,Objective-C,Swift。

什么是类?

在OOP中,类是对象的蓝图。

例如,现实世界中的汽车具有某些特性和行为。
汽车的一些特征是颜色,最高速度,重量,座位数,制造商,车牌等。
汽车的一些行为是它可以加速,施加折断,播放收音机,发出喇叭声等。

为了在OOP中表达汽车,我们使用class。
汽车的所有特性都成为类的属性,汽车的所有行为都成为方法。

什么是对象?

简而言之,对象是类的实例。
也就是说,如果类是建筑物的蓝图,则对象是实际建筑物。

类的属性是什么?

在OOP中,类的属性也称为类的属性。
它们是类的变量。

例如,汽车的颜色就是特征。
因此,在OOP中,它成为类汽车的属性。

什么是类的方法?

在OOP中,类的行为由方法定义。
它们是类的函数,但我们称它们为方法。

例如,加速是汽车的行为之一。
因此,在OOP中它成为类汽车的方法。

如何创建类?

我们使用class关键字在PHP中创建一个类,后跟该类的名称。

常见的编程约定是以大写字母开头类的名称。

在下面的代码中,我们将创建一个"学生"类。

class Student {
	
}

如何创建一个类的对象?

为了创建Student类的对象,我们在上面创建了一个对象,我们使用new关键字。

$stdObj = new Student();

创建类的对象时,我们将其称为类的实例。

目前,我们无法对上述对象做太多事情,因为Student类没有属性和方法。

因此,让我们进一步探讨类属性。

类的属性及其范围

类的属性就像一个变量,它具有某种范围来控制其可访问性。

一个类的每个属性可以具有三个范围-公共,私有和受保护。

公共属性

我们使用public关键字为一个类创建一个公共属性。

可以从类的内部和外部的代码的任何部分轻松访问此类属性。
我们可以从脚本的任何部分读取和修改公共属性。

class Student {
	public $name;
}

在上面的代码中,我们为Student类创建了一个公共属性$name

私有属性

我们使用关键字private来为一个类创建一个私有属性。

私有属性只能在该类内部创建的方法中访问。

class Student {
	public $name;
	private $address;
}

在上面的代码中,我们为Student类创建了一个私有属性$address

受保护属性

我们使用关键字" protected"来创建一个受保护的属性。

可从类内部(例如私有属性)以及从该类继承的任何类访问受保护的属性。

我们将在后面的教程中了解有关继承的更多信息。

class Student {
	public $name;
	private $address;
	protected $studentid;
}

在上面的代码中,我们为Student类创建了一个受保护的属性$studentid。

使用对象访问类属性

我们只能使用类对象访问公共属性。
通过对象访问私有和受保护的属性都将返回致命错误。

要使用对象访问公共属性,我们使用箭头符号(->)。

class Student {
	public $name;
	private $address;
	protected $studentid;
}

$stdObj = new Student();

//accessing public property
$stdObj->name = "Alice";

//this will give error
$stdObj->address = "123 Street";

//this will also give error
$stdObj->studentid = "s01";

静态属性

与静态变量一样,我们也可以在属性名称前使用" static"关键字为类创建静态属性。

静态属性与类的任何对象无关,并且可以仅使用类名后跟::运算符进行访问。

class Sample {
	public static $code = 10;
}

echo Sample::$code;		//this will print 10

类常量

我们使用const关键字来创建类常量。
顾名思义,它们的价值永远不变。

我们通常遵循在命名常量时使用所有大写字母的约定。

像静态属性一样,我们可以使用后跟::操作符的类名来访问类常量。

class Sample {
	const CODE = 10;
}

echo Sample::CODE;		//this will print 10

方法及其范围

我们使用关键字" function"创建类方法。
方法的范围控制其可访问性。

类的每种方法可以具有三个范围-公共,私有和受保护。

如果未设置方法的范围,则认为该方法是公共的。

公共方法

我们使用public关键字创建一个公共函数。

可以从类的内部和外部的任何位置访问类的公共方法。

class Sample {
	public function foo() {

	}
}

私有方法

我们使用关键字private来为一个类创建一个私有方法。

只能从同一类的其他方法访问私有方法。

class Sample {
	private function foo() {

	}
}

受保护的方法

我们使用protected关键字为类创建一个受保护的方法。

受保护的方法只能从相同类或者从该类继承的类中的其他方法访问。

class Sample {
	protected function foo() {

	}
}

使用对象调用类的方法

我们使用->符号调用使用该对象的类的方法。
在下面的示例中,我们有一个带有公共方法foo()的Sample类。
我们将创建一个对象$obj并调用将显示" Hello World!"的方法。

class Sample {
	
	public function foo() {
		printf("Hello World!");
	}
}

$obj = new Sample();

$obj->foo();		//this will print "Hello World!"

如果我们尝试使用该类的对象调用私有方法或者受保护的方法,则会发生致命错误。

class Sample {
	
	private function disp1() {
		printf("Hello World!");
	}

	protected function disp2() {
		printf("Hello World!");
	}
}

$obj = new Sample();

$obj->disp1();		//this will give error
$obj->disp2();		//this will give error

将参数传递给方法

在下面的示例中,我们将参数传递给Sample类的公共方法foo()。

class Sample {
	
	public function foo($text) {
		printf("Hello %s!", $text);
	}
}

$obj = new Sample();

$obj->foo("theitroad");		//this will print "Hello theitroad!"

方法返回值

在下面的示例中,类Sample的公共方法foo()在被调用时返回一个值。

class Sample {
	
	public function foo($text) {
		return sprintf("Hello %s!", $text);
	}
}

$obj = new Sample();

$str = $obj->foo("theitroad");		//this will return "Hello theitroad!"

printf($str);		//this will print "Hello theitroad!"

$this变量

要从同一个对象的方法中访问对象的属性和方法,我们使用$this特殊变量。

从方法访问属性

在下面的示例中,我们有一个Student类的私有属性$name。
我们将设置该值,然后使用该类的两个公共方法显示它。

//define the class
class Sample {
	
	//property
	private $name;

	//methods

	public function setName($val) {

		$this->name = $val;		//this will set the value of property $name

	}

	public function displayName() {
		printf("Name = %s", $this->name);
	}

}

//instantiating the class Sample
$obj = new Sample();

//set the name
$obj->setName("theitroad");

//display the name
$obj->displayName();

从类内部调用方法

要从属于类的另一个方法中调用一个方法,我们使用$this特殊变量。

在下面的示例中,我们定义了一个具有私有属性$name的Sample类。
我们使用公共方法setName()设置$name的值。
然后从该方法内部调用私有方法displayName()。

//define the class
class Sample {
	
	//property
	private $name;

	//methods

	public function setName($val) {

		$this->name = $val;		//this will set the value of property $name

		$this->displayName();	//calling the private method to display $name

	}

	private function displayName() {
		printf("Name = %s", $this->name);
	}

}

//instantiating the class Sample
$obj = new Sample();

//set the name and display
$obj->setName("theitroad");

静态方法

我们使用" static"关键字创建一个静态方法。
就像静态属性一样,可以使用类名后跟::运算符来访问静态方法。

class Sample {
	public static function foo() {
		printf("Hello World!");
	}
}

echo Sample::foo();	//calling the static method

静态方法与该类连接,并且由该类的所有对象共享。

构造函数

构造函数是类的一种特殊方法,该方法在创建对象后立即被调用。
我们使用具有特殊名称__construct()的方法为类创建构造函数。
构造函数通常用于初始化数据。

class Sample {
	
	//property
	private $name;

	//constructor
	function __construct() {
		$this->name = "Not set";
	}

	//methods
	public function setName($val) {
		$this->name = $val;
	}

	public function getName() {
		return $this->name;
	}

}

//creating object
//this will also call the constructor
$obj = new Sample();

printf("Name = %s", $obj->getName());	//this will print "Name = Not set" because of __constructor call

//now set name
$obj->setName("theitroad");

printf("Name = %s", $obj->getName());	//this will print "Name = theitroad"

在上面的代码中,我们创建了一个具有私有属性$name的类Sample,并使用了setName(),getName()和构造函数之类的公共方法。

当我们创建一个对象(即Sample类的实例)时,将调用构造函数__construct()。
它将私有属性$name的值设置为"未设置",然后将其打印出来。
接下来,我们设置$name的值,并使用setName()和getName()方法进行打印。

Name = theitroad

析构函数

析构函数是类的一种特殊方法,该方法在将对象从内存中删除之前被调用。
我们使用具有特殊名称__destruct()的方法为类创建析构函数。
析构函数通常用于执行清除操作,如关闭数据库连接,释放资源(如文件)等。

class Sample {
	
	//property
	private $name;

	//methods
	public function setName($val) {
		$this->name = $val;
	}

	public function getName() {
		return $this->name;
	}

	//destructor
	function __destruct() {
		printf("Destructor called...");
	}

}

//creating object
//this will also call the constructor
$obj = new Sample();

//now set name
$obj->setName("theitroad");

printf("Name = %s", $obj->getName());	//this will print "Name = theitroad"

在上面的代码中,我们创建了一个Sample类,该类具有私有属性$name和一些公共方法,如setName(),getName()和一个析构函数。

我们首先创建一个对象,即Sample类的实例,然后设置$name的值并使用setName()和getName()方法进行打印。
在对象退出之前,将调用析构函数。

Name = theitroad
Destructor called...