PGoCareerGoCareer prep tools
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

PHP · Theory

Classes and Objects

← All stacks

Theory

55/352

Classes and Objects

class Student { public $name; function __construct($name) { $this->name = $name; } } $s = new Student('Asha'); Object is one student. Class is the blueprint. $this is that object.

public/private/protected control who can touch fields. Keep fields private when you can.

Trap — $s.method() like JS. PHP is $s->method(). Trap: forgetting new.

Exam tip

new Student('Asha'); $s->name $this inside the class.

Example

<?php
class User {
  public function __construct(private string $name) {}
  public function hello(): string {
    return "Hello, {$this->name}";
  }
}
echo (new User("Ravi"))->hello();

Classes and Objects — constructor property promotion keeps simple classes short.

Short notes

  • DefClass = blueprint. Object = new Student.
  • Rule$this and -> not .
  • TrapDot instead of ->.

Questions

1

How do you make an object?

55 / 352

P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.