


Objectives of this article:
1. Understand the definition and function of static
2. Master the usage and characteristics of static
us To learn a piece of knowledge, you can learn it based on the ideas of 3w1h. Let’s briefly introduce 3w1h
3w1h that is
● what (what)
● why (why use it , what is its function)
● where (usage scenario)
● how (specifically how to use)
(1) Definition of static keyword (what)
1. Properties or methods modified by static are called static members of the class
(2) The role of the static keyword (why)
1. Let all instances of a class share a certain attribute or method.
(3). Static usage scenarios (where)
1. When you want a certain method or attribute to be When shared by all instances, you can consider static
(4), detailed use of static (how)
Summary:
1. Static To define an attribute, add static directly before the attribute definition, such as static public $name ;
2. Static attributes cannot be obtained by instances of classes, but can be obtained in the following ways:
● Class Name::$Attribute name
● Inside the class, you can pass self::$Attribute name
3. To define a static method, add static directly before the method definition, such as static public function Hello(){ }
4. Static methods cannot be obtained using class instances, but can be obtained in the following ways:
● Class name::Method name
● Inside the class, you can use self::method name
5. In PHP, you cannot use static to modify the class, you can only modify the properties or methods
6. Inside the static method Non-static properties cannot be called, only static properties
7. Non-static methods cannot be called inside static methods, only static methods
8. Non-static methods can be called inside non-static methods Static properties can also be called static properties
9. Within non-static methods, both non-static methods and static methods can be called
All the summaries are based on practice Come, then next, we use actual code to demonstrate each summary above to see if they are correct
(4), specific code
Case 1:
Practical goals:
1. To define static attributes, add static directly before the attribute definition, such as static public $name;
2. Static attributes cannot use classes Instead of obtaining the instance of
The running result is: Case 2:1. Static method Definition, add static directly before the method definition, such as static public function Hello(){ }
2. Static methods cannot be obtained using instances of the class, but can be obtained in the following ways: ● Class name:: Method name● Inside the class, you can pass self:: Method name
<?php class Human{ static public $name = "人类";//静态属性的定义 public function say(){ echo "self::name = ".self::$name ."<br/>"; } } //输出静态属性 echo "名称为:".Human::$name."<br/>"; $human = new Human(); $human->say(); ?>
and the result is:
1. In PHP, you cannot use static to modify classes, only properties or methods
<?php class Human{ public function __construct(){ self::staticFun1(); } static public function staticFun1(){ echo "我是静态方法<br/>"; } } //输出静态方法 Human::staticFun1(); //运行构造函数,看是否可以被正常调用 $human = new Human(); ?>The running result is: Parse error: syntax error, unexpected 'class' (T_CLASS), expecting :: (T_PAAMAYIM_NEKUDOTAYIM) in D:\E-class\class-code\classing\index. php on line 2
Case 4:
Practical goal:1. Non-static properties cannot be called inside static methods, only static properties can be called
<?php static class Human{ } ?>The running result is: Static attribute-humanFatal error: Uncaught Error: Using $this when not in object context in D:\E-class\class -code\classing\index.php:8 Stack trace: #0 D:\E-class\class-code\classing\index.php(18): Human::staticFun1() #1 {main} thrown in D: \E-class\class-code\classing\index.php on line 8
Case 5:
1. Static Non-static methods cannot be called inside the method, only static methods
<?php class Human{ static public $staticName = "静态属性-人类"; public $commonName="非静态属性-人类"; //定义静态方法 静态方法调用非静态属性 static public function staticFun1(){ echo $this->commonName."<br/>"; } //测试静态方法调用静态属性 static public function staticFun2(){ echo self::$staticName."<br/>"; } } Human::staticFun2();//OK Human::staticFun1();//not OK ?>The running result is:
I am a static method 2
Fatal error: Uncaught Error: Using $this when not in object context in D:\E-class\class-code\classing\index.php:8 Stack trace: #0 D:\E-class\class-code\classing\index.php(20): Human::staticFun1() #1 {main} thrown in D:\E-class\class-code\classing\index.php on line 8
案例六:
实践目标:
1、非静态方法内部,既可以调用非静态属性也可以调用静态属性
<?php class Human{ static public $staticName = "静态属性-人类"; public $name = "非静态属性-人类"; ///普通方法 public function commonFun1(){ echo self::$staticName."<br/>"; echo $this->name."<br/>"; } } $human = new Human(); $human->commonFun1(); ?>
运行结果为:
静态属性-人类
非静态属性-人类
案例七:
实践目标:
1、非静态方法内部,既可以调用非静态方法也可以调用静态方法
<?php class Human{ ///普通方法 public function commonFun1(){ self::staticFun1(); $this->commonFun2(); } //测试静态方法调用 静态方法 static public function staticFun1(){ echo "我是静态方法1<br/>"; } public function commonFun2(){ echo "我是普通方法2<br/>"; } } $human = new Human(); $human->commonFun1(); ?>
运行结果为:
我是静态方法1
我是普通方法2
(五)、学以致用
问题:
1、所有的NBA球员都有一个共同的联盟总裁,David Stern(大卫*斯特恩)
2、总裁换成了“Adam Silver” 怎么办?
大家自己思考一下,再看后面的结果
.........................
答案揭晓:
思路分析:
1、“换”是一个动词,换总裁,所以是一个方法,而总裁是一个数据,所以是一个属性
2、换总裁要达到一个目的就是,换了以后,这个对象仍然要被其他所有的NBA球员对象使用到
3、既然 总裁 (属性) 要被所有的NBA球员对象 共享,那么我们就可以结合static的作用,将总裁属性定义为静态属性
4、所以根据综上所述,大概的思路就是定义一个NBA球员类,然后类里面主要有静态属性“总裁”和一个 换总裁 的方法
具体代码如下:
<?php //Nba球员类 class NbaPlayer{ public $name = ""; //构造函数初始化对象 public function __construct($name){ $this->name = $name; } //总裁 static public $president = "David Stern"; //换总裁方法 public function changePresident($name){ self::$president = $name; } } $jordon = new NbaPlayer("乔丹"); $kebo = new NbaPlayer("科比"); echo "输出他们目前共同的总裁,总裁为:".NbaPlayer::$president."<br/>"; echo "现在把乔丹总裁换成Adam Silver<br/>"; $jordon->changePresident("Adam Silver"); echo "输出科比的总裁是否也和乔丹的一样,科比总裁为:".NbaPlayer::$president."<br/>"; ?>
运行结果为:
输出他们目前共同的总裁,总裁为:David Stern
现在把乔丹总裁换成Adam Silver
输出科比的总裁是否也和乔丹的一样,科比总裁为:Adam Silver
总结:
1、本文主要是讲了static关键字的定义和特点
希望本文能给大家带来一定的帮助,谢谢!!!
The above is the detailed content of Detailed explanation of the Static keyword in object-oriented PHP (code example). For more information, please follow other related articles on the PHP Chinese website!

如何使用Go语言实现面向对象的事件驱动编程引言:面向对象的编程范式被广泛应用于软件开发中,而事件驱动编程是一种常见的编程模式,它通过事件的触发和处理来实现程序的流程控制。本文将介绍如何使用Go语言实现面向对象的事件驱动编程,并提供代码示例。一、事件驱动编程的概念事件驱动编程是一种基于事件和消息的编程模式,它将程序的流程控制转移到事件的触发和处理上。在事件驱动

c语言static的作用和用法:1、变量作用域;2、生命周期;3、函数内部;4、修饰全局变量;5、修饰函数;6、其他用途;详细介绍:1、变量作用域,当一个变量前有static关键字,那么这个变量的作用域被限制在声明它的文件内,也就是说,这个变量是“文件级作用域”,这对于防止变量的“重复定义”问题很有用;2、生命周期,静态变量在程序开始执行时初始化一次,并在程序结束时销毁等等。

go语言既不是面向对象,也不是面向过程,因为Golang并没有明显的倾向,而是更倾向于让编程者去考虑该怎么去用它,也许它的特色就是灵活,编程者可以用它实现面向对象,但它本身不支持面向对象的语义。

python是面向对象的。Python语言在设计之初,就定位为一门面向对象的编程语言,“Python中一切皆对象”就是对Pytho 这门编程语言的完美诠释。类和对象是Python的重要特征,相比其它面向对象语言,Python很容易就可以创建出一个类和对象;同时,Python也支持面向对象的三大特征:封装、继承和多态。

一、static 请先看下面这段程序:publicclassHello{publicstaticvoidmain(String[]args){//(1)System.out.println("Hello,world!");//(2)}}看过这段程序,对于大多数学过Java的从来说,都不陌生。即使没有学过Java,而学过其它的高级语言,例如C,那你也应该能看懂这段代码的意思。它只是简单的输出“Hello,world”,一点别的用处都没有,然而,它却展示了static关键字的主

PHP作为一种广泛使用的编程语言,已成为构建动态网站和网络应用程序的首选语言之一。其中,面向对象编程(OOP)的概念和技术越来越受到开发者的欢迎和推崇。本篇文章将为读者提供PHP面向对象编程的入门指南,介绍OOP的基本概念,语法和应用。什么是面向对象编程(OOP)?面向对象编程(Object-OrientedProgramming,简称OOP),是一种编程

面向对象是软件开发方法,一种编程范式。是一种将面向对象的思想应用于软件开发过程并指导开发活动的系统方法。这是一种基于“对象”概念的方法论。对象是由数据和允许的操作组成的包,它与目标实体有直接的对应关系。对象类定义了一组具有类似属性的对象。面向对象是基于对象的概念,以对象为中心,以类和继承为构建机制,认识、理解和描绘客观世界,设计和构建相应的软件系统。

C语言中static关键字的实际应用场景及使用技巧一、概述static是C语言中的一个关键字,用于修饰变量和函数。它的作用是改变其在程序运行过程中的生命周期和可见性,使得变量和函数具有静态的特性。本文将介绍static关键字的实际应用场景及使用技巧,并通过具体的代码示例进行说明。二、静态变量延长变量的生命周期使用static关键字修饰局部变量可以将其生命周期


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

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 English version
Recommended: Win version, supports code prompts!

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools