What is method and property in php?

Class member variables are called properties. They may be referred to using other terms such as fields, but for the purposes of this reference properties will be used. They are defined by using at least one modifier [such as Visibility, Static Keyword, or, as of PHP 8.1.0, readonly], optionally [except for readonly properties], as of PHP 7.4, followed by a type declaration, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value.

Note:

An obsolete way of declaring class properties, is by using the var keyword instead of a modifier.

Note: A property declared without a Visibility modifier will be declared as public.

Within class methods non-static properties may be accessed by using -> [Object Operator]: $this->property [where property is the name of the property]. Static properties are accessed by using the :: [Double Colon]: self::$property. See Static Keyword for more information on the difference between static and non-static properties.

The pseudo-variable $this is available inside any class method when that method is called from within an object context. $this is the value of the calling object.

Example #1 Property declarations

Note:

There are various functions to handle classes and objects. See the Class/Object Functions reference.

Type declarations

As of PHP 7.4.0, property definitions can include Type declarations, with the exception of callable.

Example #2 Example of typed properties

The above example will output:

Typed properties must be initialized before accessing, otherwise an Error is thrown.

Example #3 Accessing properties

The above example will output:

string[8] "triangle"
int[3]
string[6] "circle"

Fatal error: Uncaught Error: Typed property Shape::$numberOfSides must not be accessed before initialization

Readonly properties

As of PHP 8.1.0, a property can be declared with the readonly modifier, which prevents modification of the property after initialization.

Example #4 Example of readonly properties

Note:

The readonly modifier can only be applied to typed properties. A readonly property without type constraints can be created using the mixed type.

Note:

Readonly static properties are not supported.

A readonly property can only be initialized once, and only from the scope where it has been declared. Any other assignment or modification of the property will result in an Error exception.

Example #5 Illegal initialization of readonly properties

Note:

Specifying an explicit default value on readonly properties is not allowed, because a readonly property with a default value is essentially the same as a constant, and thus not particularly useful.

Note:

Readonly properties cannot be unset[] once they are initialized. However, it is possible to unset a readonly property prior to initialization, from the scope where the property has been declared.

Modifications are not necessarily plain assignments, all of the following will also result in an Error exception:

However, readonly properties do not preclude interior mutability. Objects [or resources] stored in readonly properties may still be modified internally:

Anonymous

10 years ago

In case this saves anyone any time, I spent ages working out why the following didn't work:

class MyClass
{
    private $foo = FALSE;

    public function __construct[]
    {
        $this->$foo = TRUE;

        echo[$this->$foo];
    }
}

$bar = new MyClass[];

giving "Fatal error: Cannot access empty property in ...test_class.php on line 8"

The subtle change of removing the $ before accesses of $foo fixes this:

class MyClass
{
    private $foo = FALSE;

    public function __construct[]
    {
        $this->foo = TRUE;

        echo[$this->foo];
    }
}

$bar = new MyClass[];

I guess because it's treating $foo like a variable in the first example, so trying to call $this->FALSE [or something along those lines] which makes no sense. It's obvious once you've realised, but there aren't any examples of accessing on this page that show that.

anca at techliminal dot com

7 years ago

You can access property names with dashes in them [for example, because you converted an XML file to an object] in the following way:

Anonymous

11 years ago

$this can be cast to array.  But when doing so, it prefixes the property names/new array keys with certain data depending on the property classification.  Public property names are not changed.  Protected properties are prefixed with a space-padded '*'.  Private properties are prefixed with the space-padded class name...



This is documented behavior when converting any object to an array [see PHP manual page].  All properties regardless of visibility will be shown when casting an object to array [with exceptions of a few built-in objects].

To get an array with all property names unaltered, use the 'get_object_vars[$this]' function in any method within class scope to retrieve an array of all properties regardless of external visibility, or 'get_object_vars[$object]' outside class scope to retrieve an array of only public properties [see: PHP manual page].

zzzzBov

12 years ago

Do not confuse php's version of properties with properties in other languages [C++ for example].  In php, properties are the same as attributes, simple variables without functionality.  They should be called attributes, not properties.

Properties have implicit accessor and mutator functionality.  I've created an abstract class that allows implicit property functionality.



after extending this class, you can create accessors and mutators that will be called automagically, using php's magic methods, when the corresponding property is accessed.

kchlin dot lxy at gmail dot com

4 days ago

From PHP 8.1
It's easy to create DTO object with readonly properties and promoting constructor
which easy to pack into a compact string and covert back to a object.

Chủ Đề