Skip to main content
added 274 characters in body
Source Link
Jaypan
  • 18k
  • 3
  • 34
  • 65

The above class has athe property, $bundle, and athe method (function), bundle(). The method returns the value stored in the property.

The above enforces that username is both a string, and is the right length and only contains allowed characters.

Edit: Additional comment from Leigh:

another reason to avoid accessing the property directly is when you have multiple values. e.g. if you have a field "my_field" on a node which has multiple values if you use $node->my_field->value you'll only get 1 value returned

The above class has a property, $bundle and a method (function), bundle(). The method returns the value stored in the property.

The above enforces that username is both a string, and is the right length and only contains allowed characters.

The above class has the property $bundle, and the method (function) bundle(). The method returns the value stored in the property.

The above enforces that username is both a string, and is the right length and only contains allowed characters.

Edit: Additional comment from Leigh:

another reason to avoid accessing the property directly is when you have multiple values. e.g. if you have a field "my_field" on a node which has multiple values if you use $node->my_field->value you'll only get 1 value returned

Source Link
Jaypan
  • 18k
  • 3
  • 34
  • 65

bundle is the property on the object. It's where the value is stored. Properties can be assigned one of three ways:

  • public: these properties can be directly set or read on the object. Example:

      $object->key = 'value';
      $value = $object->key;
    
  • private: These can only be accessed by methods (functions) of the class itself. Nothing else can access it. For example, this will throw an error:

      $value = $object->key;
    

    Whereas this would be allowed:

      class Example {
        private $key;
    
        function getPrivateValue() {
          return $this->key;
        }
      }
    
      $value = $object->getPrivateValue();
    

    $key cannot be accessed outside of the class, since it is private. This can be used to enforce rules on the types of data to be stored in the property. Checks can be run to ensure it's an allowed data type.

  • protected: These are like private properties, however if a class extends the class, the extended class can access the property. If the property is private, extended classes cannot access it.

One thing for you to understand is that $node->bundle and $node->bundle() are not the same things. The first is a property, the other is a method. This should help understand, as a (non-real) example:

class Node {
  protected $bundle;

  public function bundle() {
    return $this->bundle;
  }
}

The above class has a property, $bundle and a method (function), bundle(). The method returns the value stored in the property.

Look at the error you are seeing: Cannot access protected property Drupal\field\Entity\FieldConfig::$bundle. This is because you are trying to access the $bundle property, but it is protected, meaning you cannot access it from outside the class.

To get the bundle of a node, you need to call the bundle() method on it, which will give you the bundle type. This is the answer to your question about which to use.

And in a more general point of view, are there some guidelines when to use functions and when to use variables?

It's generally thought to be a best practice to not provide direct access to properties on objects, and instead create setter and getter methods (functions) on objects to set and retrieve the property values. That way you can say, enforce a pattern:

class Example {

  protected $username

  public function setUsername($username) {
    if (!is_string($username)) {
      throw new \Exception('Username must be a string');
    }
    elseif (!preg_match('/^[a-zA-Z0-9_-]{3,12}$/', $username)) {
      throw new \Exception('Username must be between 3 and 12 characters, and may contain only alphanumerics, underscores or hyphens');
    }

    $this->username = $username;
  }

  public function getUsername() {
    return $this->username;
  }
}

The above enforces that username is both a string, and is the right length and only contains allowed characters.