Theory
Portal Loose Functions से बड़ा हो जाता है
Event portal में अब scattered variables और functions हैं: एक event का name यहाँ, इसकी seats वहाँ, एक validation function कहीं और। आपने यह smell पहले महसूस किया है, BCA403 (Java) और BCA404 (VB.NET) में: related data और behaviour एक साथ, एक class में belong करते हैं।
PHP fully object-oriented है, और अच्छी news यह है कि CONCEPTS exactly वही हैं जो आप पहले से जानते हैं: classes, constructors, visibility, inheritance, exceptions। सिर्फ़ SYNTAX नया है ($this->, __construct, extends)। यह lesson portal को proper structure और robust error handling देता है।
Theory
Classes, PHP के तरीके से
एक class properties और methods group करता है:
- constructor special method `__construct()` (2 underscores) है,
newपर automatically चलता है - methods के अंदर, object के अपने members को `$this->property` से refer कीजिए (ARROW
->, dot नहीं) - visibility:
private(सिर्फ़ यह class),protected(class + subclasses),public(हर जगह): Java जैसे ही same 3 levels - inheritance:
class VipEvent extends Event { }, method OVERRIDING के साथ और parent version call करने के लिएparent::method()
new से instantiate कीजिए: $e = new Event("Garba Night", 350);। अगर यह अलग punctuation वाला BCA403 जैसा लगे, यह exactly right है।
Practical
एक Validating 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 Validation
Exception handling PHP names वाला BCA403 grammar है:
try { ... } catch (Exception $e) { echo $e->getMessage(); } finally { ... }
throw new Exception("message") एक raise करता है; matching catch इसे handle करता है; finally ALWAYS चलता है (cleanup), चाहे एक exception हुआ हो या नहीं। वही flow जो आपने Java में trace किया था: risky code चलता है, एक exception catch में jump करता है, finally regardless execute होता है।
Regex validation preg_match("/pattern/", $string) इस्तेमाल करता है, जो 1 return करता है अगर string pattern से MATCH करे। यह है कि आप FORMATS कैसे validate करते हैं: एक 10-digit phone number, एक specific code। Regex plus filter_var (पिछले lessons) ज़्यादातर input-validation needs cover करते हैं।
Quiz
एक PHP class method के अंदर, आप object की अपनी 'name' property कैसे access करते हैं?
- this.name (dot notation, Java की तरह)
- $this->name (arrow operator)
- $name directly
- self.name
Show the answer
$this->name (arrow operator)
PHP $this->name इस्तेमाल करती है: special variable $this current object को refer करता है, और ARROW operator (->) इसके members access करता है। Option A Java/JavaScript का DOT notation इस्तेमाल करता है, जो PHP object members के लिए इस्तेमाल नहीं करती (एक बहुत common cross-language slip)। Option C ($name अकेला) एक local variable होता, property नहीं। Option D (self.name) PHP syntax नहीं है (self:: exist करता है पर static members के लिए, :: के साथ)। याद रखने के लिए 2 PHP-specific चीज़ें: current object के लिए $this (dollar sign के साथ), और इसकी properties और methods तक पहुँचने के लिए -> (arrow)।
Think first
अपनी Java OOP को PHP में Translate कीजिए
BCA403 से आप जानते हैं: constructor, this.field, extends, try/catch/finally, e.getMessage()। हर एक की PHP spelling लिखिए। फिर tap कीजिए।
Show the answer
Constructor: __construct() (2 underscores, एक special name, class name नहीं)। this.field: $this->field ($this variable, arrow operator)। extends: extends (Java जैसा identical)। try/catch/finally: try / catch (Exception $e) / finally (same keywords, typed catch variable $e नोट कीजिए)। e.getMessage(): $e->getMessage() (फिर arrow)। Concepts: public/private/protected से encapsulation, inheritance, overriding, parent::, exceptions: Java के IDENTICAL हैं; सिर्फ़ punctuation बदला ($, ->, __construct)। यह OOP को properly एक बार सीखने का payoff है: आपकी चौथी OOP language को एक lesson लगा, क्योंकि आप syntax सीख रहे थे, concepts नहीं।
Watch out
PHP OOP Traps
Arrow की बजाय Dot: PHP $this->name इस्तेमाल करती है, कभी this.name नहीं।
ग़लत Constructor Name: यह __construct() है (2 underscores), class के नाम पर एक method नहीं (वह पुराना PHP 4 style है)।
$this पर $ भूलना: यह एक variable है: $this।
Uncaught Exceptions: बिना matching catch वाला एक exception एक fatal error है; risky code को try में wrap कीजिए।
preg_match Return: यह 1 (match), 0 (no match) या false (error) return करता है: 1 check कीजिए, boolean true मत मान लीजिए।
Theory
Unit 2 Complete: एक Structured, Safe Portal
अब portal के पास files, JSON, sessions, email, classes और error handling है: यह एक real, organised PHP application है। पर यह अभी भी flat files में data store करता है, जो scale नहीं करता। Unit 3 इसे full CRUD और prepared statements वाले एक proper database (MySQL) से जोड़ता है, एक live search के लिए AJAX add करता है, और इसे CodeIgniter MVC framework पर rebuild करता है, एक PHP app structure करने का professional तरीका। Scripts से एक framework तक।
Summary
Key takeaways
- PHP fully object-oriented है: class { }, properties, methods, new से instantiate कीजिए।
- Constructor __construct() है (two underscores); अपने members को $this->property से access कीजिए (arrow, dot नहीं)।
- Visibility: private (सिर्फ़ class), protected (class + subclasses), public (हर जगह): Java जैसा ही।
- Inheritance: extends, method overriding, parent version के लिए parent::method()।
- Exceptions: try / catch (Exception $e) / finally; throw new Exception('msg'); finally हमेशा चलता है।
- preg_match('/pattern/', $string) एक match पर 1 return करता है: formats validate करने के लिए इसे इस्तेमाल कीजिए।
- Memory hook: __construct, $this->, extends: Java जैसी same OOP, नया punctuation।