Home Backend Development PHP Tutorial 探讨PHP中OO之静态关键字以及类常量的详解_php技巧

探讨PHP中OO之静态关键字以及类常量的详解_php技巧

May 17, 2016 am 09:02 AM
class constant

闲着没事随便找了点PHP的关于OO的点资料看看,访问控制修饰符,self,parent,const,static几个关键字,箭头操作符(书上也这么叫..即“->”),范围解析操作符(即双冒号“::”),不过觉得这个跟C#语言里的OO很多是一样的,很容易理解不过还是看看理清PHP里的OO思路还是应该的吧。
--------------------------------------------------------------------------------
声明静态的类的成员和方法,使它不需要一个类的实例.一个static成员的声明不能通过一个类对象的实例来访问(尽管一个静态方法可以)。
静态声明必须在可见性声明之后。为了兼容PHP 4,如果没有可见性被声明,那么成员和方法将被当作是已经声明为public。
由于静态方法可以调用非对象实例,伪变量$this不可以在声明为静态的方法中使用。
事实上static方法调用形式在编译时被确定。当使用必须要声明的类名时,方法是完全标识和无继承规则的应用。当使用必须要声明的类名时,这种方法就被完全确认,而且没有使用继承的规则。
如果self已经被声明,那么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
// $foo::my_static is not possible
print Bar::$my_static."/n";
$bar = new Bar();
print $bar->fooStatic()."/n";
?>

复制代码 代码如下:

//静态方法实例(Static method example)
class Foo
{   public static function aStaticMethod() {    }
}
Foo::aStaticMethod();
?>

可以在每个基类中定义常量使它保持不变。在你不使用$符号去声明或使用它时,常量不同于普通变量。就象静态成员,常量值不能通过对象的实例来访问(而应使用$object::constant). 常量值必须是一个常量表达式,而不是一个变量,一个类的成员,一个数学表达式或函数调用的结果。
复制代码 代码如下:

class MyClass
{   const constant = 'constant value';
    function showConstant() {   echo  self::constant."/n";   }
}
echo MyClass::constant."/n";
$class = new MyClass();
$class->showConstant();// echo $class::constant; is not allowed
?>

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

11 Best PHP URL Shortener Scripts (Free and Premium) 11 Best PHP URL Shortener Scripts (Free and Premium) Mar 03, 2025 am 10:49 AM

Long URLs, often cluttered with keywords and tracking parameters, can deter visitors. A URL shortening script offers a solution, creating concise links ideal for social media and other platforms. These scripts are valuable for individual websites a

Introduction to the Instagram API Introduction to the Instagram API Mar 02, 2025 am 09:32 AM

Following its high-profile acquisition by Facebook in 2012, Instagram adopted two sets of APIs for third-party use. These are the Instagram Graph API and the Instagram Basic Display API.As a developer building an app that requires information from a

Working with Flash Session Data in Laravel Working with Flash Session Data in Laravel Mar 12, 2025 pm 05:08 PM

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

Build a React App With a Laravel Back End: Part 2, React Build a React App With a Laravel Back End: Part 2, React Mar 04, 2025 am 09:33 AM

This is the second and final part of the series on building a React application with a Laravel back-end. In the first part of the series, we created a RESTful API using Laravel for a basic product-listing application. In this tutorial, we will be dev

Simplified HTTP Response Mocking in Laravel Tests Simplified HTTP Response Mocking in Laravel Tests Mar 12, 2025 pm 05:09 PM

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Announcement of 2025 PHP Situation Survey Announcement of 2025 PHP Situation Survey Mar 03, 2025 pm 04:20 PM

The 2025 PHP Landscape Survey investigates current PHP development trends. It explores framework usage, deployment methods, and challenges, aiming to provide insights for developers and businesses. The survey anticipates growth in modern PHP versio

See all articles