Bifalgorithm post · Computer science one · Accessor & Mutator Methods

How can PHP reference a member variable dynamically?

How can PHP reference a member variable dynamically? (Computer science one · Accessor & Mutator Methods) — "You can use the syntax $this->$foo but it will assu…

Ideas in this flowchart

  • La syntaxe $this->$foo permet d’accéder dynamiquement à une variable d’instance.
  • La syntaxe suppose que $foo contient le nom d’une autre variable.
  • $foo = "firstName" fournit un exemple de nom de variable.
  • $this->$foo se résout alors en la variable d’instance $firstName.
  • Cette fonctionnalité est utile pour les objets dont les variables sont créées dynamiquement.
  • Les variables peuvent être ajoutées lors de l’exécution.
  • Ces variables ne faisaient pas partie de la déclaration originale de la classe.
Feed/

How can PHP reference a member variable dynamically?

No cover

Channel

Computer science one

1 page

"You can use the syntax $this->$foo but it will assume that $foo is a string that contains the name of another variable, for example, if $foo = \"firstName\"; then $this->$foo would resolve to the instance’s $firstName variable. This is useful if your object has been dynamically created by adding variables at runtime that were not part of the original class declaration."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.