DEV Community

Ghulam Mujtaba
Ghulam Mujtaba

Posted on

4 1

Exception in OOP php

Exception

In OOP PHP, an exception is an object that represents an error or unexpected behavior in a program. It's a way to handle and manage errors in a more structured and organized way.

Purpose

The purpose of exceptions is to:

  • Separate error handling code from regular code
  • Provide a way to handle errors in a centralized and reusable way
  • Allow for more informative and specific error messages

How to Use Exceptions

  • Try: Wrap code that might throw an exception in a try block.

  • Throw: Use the throw keyword to create and throw an exception.

  • Catch: Use a catch block to handle an exception.

An example related to exception is:

<?php 

class TeamException extends Exception {
    public static function fromTooManyMembers() {
        return new static('You may not add more than 3 team members.');
    }
}

class Member {
    public $name;
    public function __construct($name) {
        $this->name = $name;
    }
}

class Team {
    protected $members = [];
    public function add(Member $member) {
        if(count($this->members) === 3){
            throw TeamException::fromTooManyMembers();
        }
        $this->members[] = $member;
    }
    public function members() {
        return $this->members;
    }
}

class TeamMembersController {
    public function store() {
        $team = new Team;
        try{
            $team->add(new Member('John Doe'));
            $team->add(new Member('Ghulam Mujtaba'));
            $team->add(new Member('Ali Hassan'));
            $team->add(new Member('Ahmad Ali'));
            var_dump($team->members());
        } catch(TeamException $e ) {
            var_dump($e -> get message());
        }
    }
}

(new TeamMembersController())->store();
Enter fullscreen mode Exit fullscreen mode

In this code snippet, we have a TeamException class that extends the built-in Exception class. This custom exception is used to represent an error when trying to add more than 3 members to a team.

When we run this code, it will output the exception object with the error message "You may not add more than 3 team members." This demonstrates how exceptions can be used to handle errors in a more structured and informative way.

I hope that you have clearly understood it.

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay