php5概念总结之一
/***********by garcon1986*******************/ 1. $this变量是对当前对象的引用,$this-var; $this-method(); 2. php5中的变量和常量称为property,函数称为method. 声明属性: //invalid public $var = 'hel'.'lo'; public $var2 = EOD hello EOD; public
/***********by garcon1986*******************/
1. $this变量是对当前对象的引用,$this->var; $this->method();
2. php5中的变量和常量称为property,函数称为method.
声明属性:
//invalid
public $var = 'hel'.'lo';
public $var2 =
hello
EOD;
public $var3 = 1+2;
public $var4 = self::StaticMethod();
public $var5 = $myvar;
//valid
public $var6 = myConstant;
public $var7 = array(true, false);
public $var8 =
hello
EOD;
3. 继承(inheritance): 使用extends
class A {
function A(){
}
}
class B extends A {
function B(){
}
}
4. parent:: 访问父类中的方法或属性
5. static属性或方法不需要实例化类就能访问。
self:: 访问本类的静态变量。
实例化的类对象中不能访问静态属性,但是能访问静态方法。
静态属性不能使用->访问。
静态调用非静态方法会发生E_STRICT级别的警告。
例子:
class Foo
{
public static $my_static = 'foo';
public function staticValue() {
return self::$my_static;
}
}
class Bar extends Foo
{
public function fooStatic() {
return parent::$my_static;
}
}
print Foo::$my_static . "/n";
$foo = new Foo();
print $foo->staticValue() . "/n";
print $foo->my_static . "/n"; // Undefined "Property" my_static
print $foo::$my_static . "/n";
$classname = 'Foo';
print $classname::$my_static . "/n"; // As of PHP 5.3.0
print Bar::$my_static . "/n";
$bar = new Bar();
print $bar->fooStatic() . "/n";
?>
6. Define - 定义一个有名字的常量。
define("CONSTANT","hello world");
echo CONSTANT; //输出 hello world
echo Constant; //输出 "Constant" and issues a notice.
define("GREETING", "Hello you.", true);
echo GREETING; // outputs "Hello you."
echo Greeting; // outputs "Hello you."
true会使字母不敏感,false会使字符敏感。默认是字符敏感,即false。
7. defined()用于验证某个常量是否存在。
if(defined('CONSTANT')){
echo CONSTANT; //output "hello world"
}
8. const 用于声明常量;
常量不需要使用$;
常量的值必须是常量表达式,不能是变量,属性,算术表达式,函数等。
如: const constant1 = "test of contant";
9. constant() 用于得到常量的值
define("mx",100);
echo mx;
echo constant("mx");
10. __autoload():
test1:
function __autoload($class_name) {
require_once $class_name . '.php';
}
$obj = new huanhang;
test2:
function __autoload($class_name) {
require_once $class_name . '.php';
}
class huanhang{
function huanghang(){
echo "huanhang sleep";
}
}
$obj = new huanhang();
$obj -> huanghang();
11. __construct
__construct 构造方法,当一个对象创建时调用此方法,使用此方法的好处是:可以使构造方法有一个独一无二的名称,无论它所在的类的名称
是什么.这样你在改变类的名称时,就不需要改变构造方法的名称
__destruct 析构方法,PHP将在对象被销毁前(即从内存中清除前)调用这个方法
12. abstract: PHP5中推出了抽象类和抽象方法。
抽象类不能被实例化。
任何类包含了一个以上的抽象方法,这个类也必须声明为抽象类。
继承抽象类时,父类中所有的抽象方法必须在子类中定义;而且这些方法的可见性必须跟父类的方法保持一致或者更少的限制。如:父类的方
法是protected,子类的方法就得是protected或public。
13. interface :
接口通过interface关键字定义,但是其中的方法不需要定义内容。
接口中所有的方法必须为public
所有的方法必须在类中实现implemented,否则会产生严重错误。
一个类不能实现两个接口,因为会造成混淆。
interface也能通过extends被继承
14. visibility可见性
类的属性有public,protected,private. 通过var定义等同于public
类的方法有public,protected,private. 默认是public
15. overloading重载:指动态的创建类属性和方法。通过魔术函数实现。
所有的重载方法都必须被声明为public。
PHP5提供了一种迭代(iteration)对象的功能,就像使用数组那样,可以通过foreach 来遍历对象中的属性。默认情况下,在外部迭代只能
得到外部可见的属性的值。
16. design pattern 设计模式
工厂模式(factory)允许你在代码执行时实例化对象。
单例模式(singleton)用于为一个类生成一个唯一的对象.最常用的地方是数据库连接。
17. 魔术方法(magic method)
__construct, __destruct (参看 构造方法和析构方法), __call, __callStatic, __get, __set, __isset, __unset (参看 重载),
__sleep, __wakeup, __toString, __set_state 和 __clone 等方法在PHP中被称为“魔术方法”(Magic methods)。
PHP把所有以__(两个下划线)开头的类方法当成魔术方法。所以你定义自己的类方法时,不要以 __为前缀。
18. Final
如果父类中的方法被声明为final,则子类无法覆盖该方法;
如果一个类被声明为final,则不能被继承。
19. clone
$copy_of_object = clone $object;

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The differences between php5 and php8 are in terms of performance, language structure, type system, error handling, asynchronous programming, standard library functions and security. Detailed introduction: 1. Performance improvement. Compared with PHP5, PHP8 has a huge improvement in performance. PHP8 introduces a JIT compiler, which can compile and optimize some high-frequency execution codes, thereby improving the running speed; 2. Improved language structure, PHP8 introduces some new language structures and functions. PHP8 supports named parameters, allowing developers to pass parameter names instead of parameter order, etc.

Summary of the system() function under Linux In the Linux system, the system() function is a very commonly used function, which can be used to execute command line commands. This article will introduce the system() function in detail and provide some specific code examples. 1. Basic usage of the system() function. The declaration of the system() function is as follows: intsystem(constchar*command); where the command parameter is a character.

The Metaverse is an illusory world that uses technology to map and interact with the real world. Analysis 1 Metaverse [Metaverse] is an illusory world that makes full use of technological methods to link and create, and maps and interacts with the real world. It is a data living space with the latest social development system. The 2-dimensional universe is essentially a virtual technology and digital process of the real world, which requires a lot of transformation of content production, economic system, customer experience and physical world content. 3 However, the development trend of the metaverse is gradual. It is finally formed by the continuous combination and evolution of many tools and platforms with the support of shared infrastructure, standards and protocols. Supplement: What is the metaverse composed of? 1 The metaverse is composed of Meta and Verse, Meta is transcendence, and V

Basic concepts and functions of Gunicorn Gunicorn is a tool for running WSGI servers in Python web applications. WSGI (Web Server Gateway Interface) is a specification defined by the Python language and is used to define the communication interface between web servers and web applications. Gunicorn enables Python web applications to be deployed and run in production environments by implementing the WSGI specification. The function of Gunicorn is to

Understand the key features of SpringMVC: To master these important concepts, specific code examples are required. SpringMVC is a Java-based web application development framework that helps developers build flexible and scalable structures through the Model-View-Controller (MVC) architectural pattern. web application. Understanding and mastering the key features of SpringMVC will enable us to develop and manage our web applications more efficiently. This article will introduce some important concepts of SpringMVC

Introduction and core concepts of OracleRAC (RealApplicationClusters) As the amount of enterprise data continues to grow and the demand for high availability and high performance becomes increasingly prominent, database cluster technology becomes more and more important. OracleRAC (RealApplicationClusters) is designed to solve this problem. OracleRAC is a high-availability, high-performance cluster database solution launched by Oracle.

How to change port 80 in php5: 1. Edit the port number in the Apache server configuration file; 2. Edit the PHP configuration file to ensure that PHP works on the new port; 3. Restart the Apache server, and the PHP application will start running on the new port. run on the port.

The Java language is one of the most commonly used object-oriented programming languages in the world today. The concept of classes is one of the most important features of object-oriented languages. A class is like a blueprint for an object. For example, when we want to build a house, we first create a blueprint of the house, in other words, we create a plan that shows how we are going to build the house. According to this plan we can build many houses. Likewise, using classes, we can create many objects. Classes are blueprints for creating many objects, where objects are real-world entities like cars, bikes, pens, etc. A class has the characteristics of all objects, and the objects have the values of these characteristics. In this article, we will write a Java program to find the perimeter and faces of a rectangle using the concept of classes
