OOP and exception handling in PHP: classes and objects; constructors and property visibility; inheritance and method overriding; exception handling (try, catch, finally, throw); input validation using regular expressions

PHP પૂરેપૂરું object-oriented છે: __construct અને public/private/protected વાળી visibility ધરાવતા classes, extends થી inheritance, ભૂલો માટે try/catch/finally, અને regex ની ચકાસણી માટે preg_match.

12 min read · 9 cards · 2 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

Portal છૂટાં functions થી મોટું થઈ ગયું

Event portal પાસે હવે વેરવિખેર variables અને functions છે: event નું નામ અહીં, એની બેઠકો ત્યાં, અને ચકાસણીનું function વળી બીજે ક્યાંક. આ ગંધ તમે પહેલાં પણ અનુભવી છે, BCA403 (Java) અને BCA404 (VB.NET) માં: સંબંધિત data અને વર્તન સાથે જ, એટલે કે class માં જ રહેવાં જોઈએ.

PHP પૂરેપૂરું object-oriented છે, અને સારા સમાચાર એ કે એના ખયાલો બરાબર એ જ છે જે તમે પહેલેથી જાણો છો: classes, constructors, visibility, inheritance, exceptions. ફક્ત syntax નવી છે ($this->, __construct, extends). આ પાઠ portal ને યોગ્ય માળખું અને મજબૂત error handling આપે છે.

Theory

Classes, PHP ની રીતે

Class properties અને methods ને એકસાથે જૂથમાં મૂકે છે:

  • Constructor એ ખાસ method `__construct()` છે (2 underscore), જે new વખતે આપોઆપ ચાલે છે
  • Methods ની અંદર object ના પોતાના members ને `$this->property` થી વાપરો (તીર ->, ટપકું નહીં)
  • Visibility: private (ફક્ત આ class), protected (class અને એના subclasses), public (બધે): Java જેવા જ 3 સ્તર
  • Inheritance: class VipEvent extends Event { }, જેમાં method ને overriding કરી શકાય અને પિતાની આવૃત્તિ બોલાવવા parent::method() વપરાય

new થી નમૂનો બનાવો: $e = new Event("Garba Night", 350);. આ તમને જુદાં વિરામચિહ્નો સાથેનું BCA403 લાગતું હોય, તો એ બરાબર જ છે.

Practical

ચકાસણી કરતા constructor વાળો Event class

<?php
  class Event {
    private $name;
    private $seats;

    public function __construct($name, $seats) {
      if ($seats < 0) {
        throw new Exception("Seats cannot be negative");
      }
      $this->name = $name;      // $this->  , arrow not dot
      $this->seats = $seats;
    }

    public function describe() {
      return $this->name . ": " . $this->seats . " seats";
    }
  }

  try {
    $e = new Event("Garba Night", 350);
    echo $e->describe();        // Garba Night: 350 seats
    $bad = new Event("Ghost", -5);   // throws
  } catch (Exception $ex) {
    echo "Error: " . $ex->getMessage();   // Error: Seats cannot be negative
  } finally {
    echo "<br>Done";           // always runs
  }
?>

Theory

Exceptions અને regex વાળી ચકાસણી

Exception handling એ PHP નાં નામ સાથેનું BCA403 વાળું જ વ્યાકરણ છે:

try { ... } catch (Exception $e) { echo $e->getMessage(); } finally { ... }

throw new Exception("message") એને ઊભું કરે છે; બંધબેસતું catch એને સંભાળે છે; finally હંમેશા ચાલે છે (સફાઈ માટે), exception આવ્યું હોય કે ન આવ્યું હોય. Java માં તમે તપાસ્યો હતો એ જ પ્રવાહ: જોખમી code ચાલે, exception આવે તો catch પર કૂદકો મરાય, અને finally તો ગમે તેમ ચાલે જ.

Regex વાળી ચકાસણી માટે preg_match("/pattern/", $string) વપરાય છે, જે string ઢાંચા સાથે બંધબેસે તો 1 પાછું આપે છે. ઘાટ ચકાસવાની આ જ રીત છે: 10 અંકનો ફોન નંબર, કોઈ ચોક્કસ સંકેત. Regex અને ગયા પાઠનું filter_var મળીને input ની ચકાસણીની મોટા ભાગની જરૂરિયાતો પૂરી કરે છે.

Quiz

PHP ના class ની method ની અંદર object ની પોતાની 'name' property સુધી કઈ રીતે પહોંચાય?

  1. this.name (Java ની જેમ ટપકાવાળી રીત)
  2. $this->name (તીરવાળો operator)
  3. સીધું $name
  4. self.name
Show the answer

$this->name (તીરવાળો operator)

PHP માં $this->name વપરાય છે: ખાસ variable $this ચાલુ object તરફ ઇશારો કરે છે, અને તીરવાળો operator (->) એના members સુધી પહોંચે છે. વિકલ્પ A Java કે JavaScript ની ટપકાવાળી રીત વાપરે છે, જે PHP object ના members માટે વાપરતું નથી (ભાષાઓ વચ્ચેની આ ખૂબ સામાન્ય ચૂક છે). વિકલ્પ C માં એકલું $name એ સ્થાનિક variable ગણાય, property નહીં. વિકલ્પ D (self.name) એ PHP ની syntax જ નથી (self:: અસ્તિત્વમાં છે, પણ static members માટે અને :: સાથે). PHP ની ખાસ 2 વસ્તુ યાદ રાખો: ચાલુ object માટે $this (ડૉલરના ચિહ્ન સાથે), અને એની properties તથા methods સુધી પહોંચવા -> (તીર).

Think first

તમારું Java વાળું OOP PHP માં ઉતારો

BCA403 માંથી તમે જાણો છો: constructor, this.field, extends, try/catch/finally, e.getMessage(). દરેકની PHP વાળી જોડણી લખો. પછી tap કરો.

Show the answer

Constructor: __construct() (2 underscore, એક જ ખાસ નામ, class ના નામ જેવું નહીં). this.field: $this->field ($this variable અને તીરવાળો operator). extends: extends (Java જેવું જ). try/catch/finally: try / catch (Exception $e) / finally (એ જ શબ્દો, પણ catch માં પ્રકાર સાથેનું $e ધ્યાનમાં રાખો). e.getMessage(): $e->getMessage() (ફરી તીર). ખયાલો, એટલે કે public/private/protected વાળું encapsulation, inheritance, overriding, parent:: અને exceptions, એ બધા Java જેવા જ છે; ફક્ત વિરામચિહ્નો બદલાયાં છે ($, ->, __construct). OOP એક વાર બરાબર શીખવાનું આ જ ફળ છે: તમારી ચોથી OOP ભાષા એક જ પાઠમાં પતી ગઈ, કારણ કે તમે ખયાલો નહીં, ફક્ત syntax શીખતા હતા.

Watch out

PHP ના OOP ના ફાંદા

તીરને બદલે ટપકું: PHP માં $this->name વપરાય છે, this.name કદી નહીં.

ખોટું constructor નું નામ: એ __construct() છે (2 underscore), class ના નામ વાળી method નહીં (એ જૂની PHP 4 ની ઢબ છે).

$this પરનું $ ભૂલી જવું: એ variable છે: $this.

નહીં પકડાયેલાં exceptions: જેને બંધબેસતું catch ન હોય એવું exception ઘાતક ભૂલ બને છે; જોખમી code ને try માં વીંટો.

preg_match શું પાછું આપે છે: એ 1 (મેળ), 0 (મેળ નહીં) કે false (ભૂલ) આપે છે: 1 ની તપાસ કરો, boolean true ધારી ન લો.

Theory

Unit 2 પૂરું: માળખાબદ્ધ અને સલામત portal

Portal પાસે હવે files, JSON, sessions, email, classes અને error handling છે: એ ખરી, વ્યવસ્થિત PHP application છે. પણ એ હજી data ને સપાટ files માં સંઘરે છે, જે મોટા પાયે ચાલે નહીં. Unit 3 એને પૂરેપૂરા CRUD અને prepared statements સાથે ખરા database (MySQL) સાથે જોડે છે, જીવંત શોધ માટે AJAX ઉમેરે છે, અને એને CodeIgniter ના MVC framework પર ફરી બાંધે છે, જે PHP ની app ને માળખું આપવાની વ્યાવસાયિક રીત છે. Scripts થી framework સુધી.

Summary

Key takeaways

  • PHP પૂરેપૂરું object-oriented છે: class { }, properties, methods, અને new થી નમૂનો બનાવવો.
  • Constructor એટલે __construct() (બે underscore); પોતાના members સુધી $this->property થી પહોંચો (તીર, ટપકું નહીં).
  • Visibility: private (ફક્ત class), protected (class અને subclasses), public (બધે): Java જેવું જ.
  • Inheritance: extends, method નું overriding, અને પિતાની આવૃત્તિ માટે parent::method().
  • Exceptions: try / catch (Exception $e) / finally; throw new Exception('msg'); finally હંમેશા ચાલે છે.
  • preg_match('/pattern/', $string) મેળ થાય તો 1 આપે છે: ઘાટ ચકાસવા એને વાપરો.
  • યાદ રાખવાની કડી: __construct, $this->, extends: Java જેવું જ OOP, ફક્ત નવાં વિરામચિહ્નો.

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Advanced PHP and File Management

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

OOP and exception handling in PHP: classes and objects; constructors and property visibility; inheritance and method overriding; exception handling (try, catch, finally, throw); input validation using regular expressions · Web Framework and Services (Major-12) · Gri-Learn