Properties of the PHP class

Source: Internet
Author: User
Tags php class
A variable member of a class is called a "property", or "field", "characteristic", which is known collectively as "Properties" in this document. A property declaration consists of the keyword public,protected or private, followed by a common variable declaration. A variable in a property can be initialized, but the initialized value must be a constant, where a constant is a PHP script that can get its value at compile time without relying on the information at run time to evaluate it.

Note:

For backwards compatibility with PHP 4,php 5 claims properties can still be substituted (or appended to) public,protected or private using the keyword var directly. However, Var is no longer needed. PHP 5.0 to 5.1.3,var is considered obsolete and throws a e_strict warning, but 5.1.3 is no longer considered obsolete and does not throw a warning.

If you use Var to declare a property without using either public,protected or private, PHP 5 treats it as public.

Inside a member method of a class, you can access a non-static property by using the (object operator): $this->property, where property is the name of the attribute. A static property is accessed by:: (Double-colon): Self:: $property.

Example #1 Property Declaration

Class Simpleclass

{

Wrong property declaration

Public $var 1 = ' hello '. ' World ';

Public $var 2 = <<<eod

Hello World

EOD;

Public $var 3 = 1+2;

Public $var 4 = Self::mystaticmethod ();

Public $var 5 = $myVar;


Correct property declaration

Public $var 6 = myconstant;

Public $var 7 = array (true,false);

PHP5.3.0 and later, the following statement is also correct

Public $var 8 = <<< ' EOD '

Hello World

EOD;

}

Unlike Heredocs, Nowdocs can be used in any static data context, including property declarations.

Example #2 Example: Initializing properties with Nowdoc

From PHP5.3.0

Public $var = <<< ' EOD '

Hello World

EOD;

Note:

Nowdoc support was added in PHP 5.3.0.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.