Object oriented programming with php in hindi

Contents

  • 1 Introduction
  • 2 What is OOP in php?
  • 3 OOPs concepts
  • 4 What is class?
  • 5 Object
  • 6 Encapsulation
  • 7 Abstraction
  • 8 Inheritance
  • 9 polymorphism

Introduction

हेलो दोस्तों आज हम इस पोस्ट में php के OOP के बारेमे जानेगे!, तो इस post में हम OOP के सभी concepts जैसे की class, objects, encapsulation, abstraction, Inheritance, polymorphism ये सभी concepts को समझेंगे!.

What is OOP in php?

OOP का fullform object-oriented programing है!. और OOP concepts php5 में introduced हुवा था और php5 और इसके बाद के php के सभी version में code को हम object oriented style में लिख सकते है!. php में OOP एक प्रकार की coding लिखने की style है!. या हम ऐसे एक coding को लिखने की methodology कह सकते है!. OOP जो concepts है वह एक real-life base concepts है!.OOP को समजे तो आप जो बी चीज को देख सकते है उसे हम एक objects के तोर पर ले सकते है!. OOP में हर एक चीज object के form में होती है!.Real-life में objects को समजने के लिए हम एक car को object कह सकते है!. उसके आलावा हम जो चीज देख सकते है जो जिसकी property और functionality बताता है तो उसे हम object कह सकते है!.

php में coding को हम दो pattern में लिख सकते है!.
1. procedural
2. Object oriented
OOP में लिखा code better तरीके से organised होता है!. OOP में code module और reusable होता है!. OOP में लिखे code में error dubug (find) करना आसान होता है!.
OOP code pattern का इस्तेमाल medium और बड़े level के project में कर सकते है!. क्यों की बड़े level के project में code को proper तरीके से manage करना होता है!. इस लिए OOP का इस्तेमाल होता है!.

  • Class
  • Object
  • Encapsulation
  • Abstraction
  • Inheritance
  • Polymorphism

What is class?

Class एक collection होता है objects के properties और method का। Classes, Objects का blueprint होता है जिसका इस्तेमाल objects के propertis और methods को रखने के लिए किया जाता है!.
PHP में class name और जिस php file में class का program लिखा जा रहा है उस file का name सामान होना चाहिए!. class एक user-defined data types है!. जिसमे do-entitis होती है!.
1.Data members and 2.Member functions

syntax:

हम खुद का class define कर सकते है “class” keywor के साथ में

syntax with data members and member functions

Class or data member of member function details

Class: class को define करने के लिए class keyword का इस्तेमाल किया जाता है!.
Class_name: यह class name defined करता है और वह user defined है वह class keyword के बाद में defined किया जाता है!.
data_variable_name: यह data member वेरिएबल का name है!. हमें var keyword का इस्तेमाल करके class में data member defined कर सकते है!.
member_function_name: यह आपकी class का member function का नाम है!. Class में function keyword का इस्तेमाल करके function को declared कर सकते है!.
Argument (parameter): यह function को declared करते time argument दिया जाता है!.

Example:

name = $name;
         }
        function get_name(){
               echo $this->name;
        }
    }
 ?>

ऊपर के example में देख सकते है की हमने car name का class defined किया है और class की अंदर में दो properties $name और $color और दो methods set_name() और get_name() defined किया है!. जिसका इस्तेमाल $name properties को set और get करने की लिए होता है!.
Class में variable को properties और functions को methods कह सकते है!.

Object

PHP में class एक सबसे important oop concepts में से एक है!. एक class में कई object हो सकते है!.Class के सभी objects class के सभी data members और member functions को access कर सकते है!. Php में किसी बी class का objects बनाने के लिए new keyword का उपयोग किया जाता है!.

Example:

name = $name;
  }
  function get_carname( $name ){
    echo $this->name = $name;
  }
  function color_name( $color ){
    $this->color = $color;
  }
  function get_name_color( $color ){
    echo $this->color = $color;
  }
}
//HERE create object
  $name = new car();
  $color = new car();

  // $name->car_name('BMW');
  // $name->color_name('RED');

  $name->get_carname('BMW');
  $name->get_name_color('red');

?>

ऊपर के example में देख सकते है की new keyword का इस्तेमाल से car class के objects $name और $color create किया है!. $name=new car(); $name object का इस्तेमाल से car का नाम $name->car_name(‘BMW’); get बी करा सकते है!.

Encapsulation

Encapsulation का मतलब data members और methods को किसी एक module ( class ) में protect रखना कह सकते है!. Encapsulation का मतलब external sources ( access ) से objects के internal details को protect करना, तो उसे ही हम encapsulation कह सकते है!.
Encapsulation class में defined किये गए!. Data variable और method को protect करने की एक protected technic है!. Encapsulation class properties private defined होते है!. इस लिए class के properties को class के बहार direct access नहीं किया जा सकता!. Simply encapsulation को class के रूप में use किया जाता है!.

Abstraction

Abstraction का मतलब ऐसा class जिसका हम object create नहीं कर सकते तो इसे हम Abstraction कह सकते है!. class name के पहले abstract keyword होना abstraction class बताता है!. Abstraction class होने लिए तीन condition होनी चाहिए पहली class name से पहले abstract keyword होना, दूसरा abstract class के अंदर एक abstract function होना चाहिए, last में abstract class का जोबी child class होता है इसके जरिये abstract class के abstraction function का implement करना जरुरी होता है!.
abstract class का हम object नहीं बना सकते, अगर abstract class की properties और method का उसे करना हो तो उसका child class बनाते है Inheritance का use करके और उसके बाद में abstract class की properties और methods को access कर सकते है!.

Inheritance

For example हमारी पास दो class है एक class A और दूसरा class B तो दोनों class की उनकी properties और method होंगे लेकिन हमें एक class की properties और method दूसरे class में use करनी होतो हम Inheritance concepts से कर सकते है!. जैसे की A class की propertie और method को B class में use करना चाहते है तो उसके लिए Inheritance का use होता है तो Inheritance का मतलब एक class की properties और method का दूसरी class में reuse करना. Php में किसी class को inherit करने के लिए extend keyword का इस्तेमाल किया जाता है!.

polymorphism

polymorphism का मतलब same name का function को अलग-अलग class में इस्तेमाल कर सकते है लेकिन function का behaviour अलग-अलग class में अलग-अलग होता है! इसे हम polymorphism कह सकते है!.