PHP构造函数

本文概述

  • PHP 5允许开发人员为类声明构造函数方法。
  • 构造函数适用于对象使用之前可能需要的任何初始化。
  • 我们可以使用” __construct”或与类名相同的名称来设计构造函数。
  • 如果子类定义了构造函数, 则不会隐式调用父构造函数。为了运行父构造函数, 请调用parent :: __ construct()。

例子1

<?php
	class Example
	{
		public function __construct()
		{
			echo "Hello srcmini";
		}
	}
	$obj = new Example();
	$obj = new Example();
?>

输出

PHP构造函数

例子2

<?php
	class demo
	{
		public function demo()
		{
			echo "constructor1...";
		}
	}
	
	class demo1 extends demo
	{
		public function __construct()
		{
			echo parent::demo();
			echo "constructor2...";
		}
	}
	$obj= new demo1();
?>

输出

PHP构造函数

来源:

https://www.srcmini02.com/29298.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_37982.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?