Skip to content
Learn Netverks

Lesson

Step 22/36 61% through track

visibility-inheritance

Visibility, inheritance, and interfaces

Last reviewed Jun 1, 2026 Content v20260601
Track mode
server_script
Means
Server runner
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Visibility, inheritance, and interfaces: the syntax, APIs, and habits you need before advancing in PHP.

Teams ship Visibility, inheritance, and interfaces on every PHP codebase—skipping it leaves gaps in debugging and code reviews.

You will apply Visibility, inheritance, and interfaces in contexts like: LAMP/LEMP stacks, Laravel apps, WordPress themes/plugins, and shared hosting.

Write PHP in the editor and click Run on server—the dev runner executes your script and returns stdout/stderr (set LEARNING_RUNNER_ENABLED=true locally).

When you can explain the previous lesson's ideas without copying starter code.

Encapsulation uses public, protected, and private. Inheritance shares behavior; interfaces define contracts without implementation.

Visibility

  • public — callable from anywhere
  • protected — class and subclasses
  • private — declaring class only

Inheritance

class Admin extends User {
    public function canPublish(): bool { return true; }
}

Single inheritance for classes; traits mix in horizontal reuse. Override methods with compatible signatures; use parent:: to call parent implementation.

Interfaces

interface Notifier {
    public function send(string $message): void;
}

Classes implement interfaces; type-hint interfaces in constructors for testability (dependency injection).

Important interview questions and answers

  1. Q: Abstract class vs interface?
    A: Abstract classes can hold state and partial implementation; interfaces are pure contracts—classes can implement many interfaces but extend one class.
  2. Q: When use final?
    A: Prevent overriding a method or extending a class when invariants must not change.

Self-check

  1. Can a class implement two interfaces?
  2. Which visibility should internal helpers use?

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Playground

Runs on the configured server runner (dev: npm run runner with LEARNING_RUNNER_ENABLED=true). Output appears below the editor.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • protected vs private?
  • Override rules?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump