Skip to main content

PHP Getter and Setter in VSCode

Implement secure PHP Getters and Setters in VSCode effortlessly. Learn the best practices for private class properties and use the recommended extension to generate them instantly.

· 3 min read
View raw
Christophe
Markdown, WSL and Docker lover ~ Insatiable curious.
Cet article existe aussi en français — lire la version française.

This article is over a year old but was reviewed on July 30, 2026 — the content is still accurate.

PHP Getter and Setter in VSCode

⚡
TL;DR

This article makes the case for private PHP class properties accessed only through getters/setters (so values can be validated, e.g. rejecting a negative price), and shows the PHP Getters & Setters VSCode extension that generates them automatically via a right-click on the property — removing the excuse that writing them is tedious.

Because you're an excellent developer, you deny anyone access to the properties of your class directly, but only via a getter or setter.

In other words, in your PHP class, you don't have public properties (they're the devil) but exclusively protected ones or better private.

And using getters and setters you allow other objects to interact with your private properties by reading them (getters) or updating their values (setters).

Rector can spot the places where a property should have been private in the first place, and Tabnine will often guess the whole setter as soon as you start typing private function set.

Some people will say "Yes, but it's tedious to write these functions", but not at all.

See the extension in action​

Right-click a property, pick an action, and the getter/setter boilerplate is generated for you:

PHP Getters & Setters

PHP Getter and Setter in VSCode

No excuse for skipping them now — the PHP Getters & Setters extension does the typing for you.

The bad scenario​

<?php
namespace Cavo;
class Product
{
public string $name = '';
public float $price = 0;
}

This defines a simple Product class in the Cavo namespace with two public properties: a name (text) and a price (decimal number). It's a basic data container that holds information about a product.

As you see $name is public so I can write things like below and it's ... OK.

$product = new Product();
$product->name='coMPuteR';
$product->price=-10;

By OK I mean, well, I've used a mix of lower and upper case for the name and, well, I've said I'm selling computers at the cost of -10€. It's OK because I don't do any validation on the values, and that is very bad.

By using a setter (a function called when the property is initialized) I can verify the value and correct the case f.i. or deny negative price.

The good way​

Let's create our class with private properties:

<?php
namespace Cavo;
class Product
{
private string $name = '';
private float $price = 0;
public function __construct()
{
}
}

This is a Product class that serves as a basic container for product data. It defines two private properties for a product's name and price, each initialized with default values, and an empty constructor that doesn't do anything yet.

So, for each property, you need to write a function called Getter to read its contents and another function called Setter to modify it. It could be a hassle, but it's not, thanks to the extension shown at the top of this article: just right-click on a property, select what you wish as action and run it.

Now, I can write:

$product = new Product();
$product->setName('coMPuteR');
$product->setPrice(-10);

And the code using the class:

//highlight-next-line
public function getName(): string
{
return $this->name;
}
//highlight-next-line
public function setName(string $name): self
{
// Force the product name to be written in lowercase
$this->name = strtolower($name);
return $this;
}
//highlight-next-line
public function getPrice(): float
{
return $this->price;
}
//highlight-next-line
public function setPrice(float $price): self
{
if ($price < 0) {
throw new \Exception("Negative prices are not correct; please review your code");
}
$this->price = $price;
return $this;
}

This is a set of getter and setter methods for a product object's name and price. The setName method converts names to lowercase, and setPrice validates that prices aren't negative before storing them. Both setters return the object itself, which allows you to chain multiple setter calls together.

Letting the editor generate this boilerplate is one way to keep your PHP clean. Letting it check your code is another: PHP development in a devcontainer with preinstalled code quality tools sets up a ready-to-use environment with linters and static analysis already in place.


Was this article helpful?
Did you try the steps in this article?