Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Aprenda Visibility: public, private, and protected | OOP Principles and Advanced Features
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Object-Oriented PHP

bookVisibility: public, private, and protected

The Idea: Controlling Access with Visibility

In PHP, you use visibility keywords to control where properties and methods can be accessed.

  • public members can be accessed from anywhere;
  • private members can only be accessed within the same class;
  • protected members can be accessed within the class and by inheriting classes.

This is important for protecting data and organizing your code. Visibility ensures that only the right parts of your program can use or change certain values or behaviors, which helps prevent bugs and keeps your code maintainable.

Code Example: Using public, private, and protected

Below, you will see a User class that demonstrates all three visibility levels for properties and methods.

User.php

User.php

copy
123456789101112131415161718192021222324252627282930313233
<?php class User { public $name; private $password; protected $role; public function __construct($name, $password, $role) { $this->name = $name; $this->password = $password; $this->role = $role; } public function getName() { // Public method: accessible from anywhere return $this->name; } private function getPassword() { // Private method: accessible only within this class return $this->password; } protected function getRole() { // Protected method: accessible within this class and subclasses return $this->role; } public function showInfo() { // Can access all members inside the class return "Name: " . $this->name . ", Role: " . $this->role; } }

The User class has three properties: public $name, private $password, and protected $role.

  • The public property can be accessed from anywhere, even outside the class;
  • The private property is only accessible inside the class itself;
  • The protected property can be accessed within the class and by any subclasses that extend it.

Similarly, methods can also have access modifiers. getName() is public and can be called from anywhere. getPassword() is private and can only be used inside the class. getRole() is protected and is available inside the class and in subclasses.

The showInfo() method demonstrates that inside the class, you can access all properties and methods, regardless of their modifiers.

question mark

Which visibility should you use if you want a property to be accessible only inside the class where it is defined?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 1

Pergunte à IA

expand

Pergunte à IA

ChatGPT

Pergunte o que quiser ou experimente uma das perguntas sugeridas para iniciar nosso bate-papo

bookVisibility: public, private, and protected

Deslize para mostrar o menu

The Idea: Controlling Access with Visibility

In PHP, you use visibility keywords to control where properties and methods can be accessed.

  • public members can be accessed from anywhere;
  • private members can only be accessed within the same class;
  • protected members can be accessed within the class and by inheriting classes.

This is important for protecting data and organizing your code. Visibility ensures that only the right parts of your program can use or change certain values or behaviors, which helps prevent bugs and keeps your code maintainable.

Code Example: Using public, private, and protected

Below, you will see a User class that demonstrates all three visibility levels for properties and methods.

User.php

User.php

copy
123456789101112131415161718192021222324252627282930313233
<?php class User { public $name; private $password; protected $role; public function __construct($name, $password, $role) { $this->name = $name; $this->password = $password; $this->role = $role; } public function getName() { // Public method: accessible from anywhere return $this->name; } private function getPassword() { // Private method: accessible only within this class return $this->password; } protected function getRole() { // Protected method: accessible within this class and subclasses return $this->role; } public function showInfo() { // Can access all members inside the class return "Name: " . $this->name . ", Role: " . $this->role; } }

The User class has three properties: public $name, private $password, and protected $role.

  • The public property can be accessed from anywhere, even outside the class;
  • The private property is only accessible inside the class itself;
  • The protected property can be accessed within the class and by any subclasses that extend it.

Similarly, methods can also have access modifiers. getName() is public and can be called from anywhere. getPassword() is private and can only be used inside the class. getRole() is protected and is available inside the class and in subclasses.

The showInfo() method demonstrates that inside the class, you can access all properties and methods, regardless of their modifiers.

question mark

Which visibility should you use if you want a property to be accessible only inside the class where it is defined?

Select the correct answer

Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 2. Capítulo 1
some-alt