Web development basic and advance tutorial, php basic tutorial, laravel basic tutorial, React Native tutorial

Thursday, January 30, 2020

PHP Object Oriented Programming Bangla Tutorial

0 comments

Ans. PHP is Object oriented programming language.
এখন প্রশ্ন হল, তাহলে Object Oriented Programming কি? Computer Programming এ class এবং Object এর ধারণাকে কাজে লাগিয়ে যে Programming করা হয় তাকেই বলা হয় Object Oriented Programming। অবজেক্ট ওরিয়েন্ট কনসেপ্ট তিনটি ধারণার উপর প্রতিষ্ঠিত:
Inheritance-ইংরেজি শব্দ inheritance অর্থ হচ্ছে উত্তরাধিকার। Object Oriented Programming এ এক class থেকে কিছু ফাংশনালিটি (Methods) ও বৈশিষ্ট্য (Property) অন্য class এ শেয়ার করার একটা পদ্ধতি হচ্ছে Inheritance. বা কোন Class বা Object এ অন্য কোন Class বা Object এর বৈশিষ্ট্য অর্জন করে থাকে যে প্রক্রিয়ায় তাকে উত্তরাধিকার সূত্র বা Inheritance বলে। আরো সহজ ভাবে বলা যায় , একটা ক্লাসকে (Parent Class) Inherit (অনুসরণ) করে তার কিছু বৈশিষ্ট্য আরেকটি উত্তরসূরি (child class) এর মধ্যে ব্যবহার করার প্রক্রিয়াকে Inheritance বলা হয়।
encapsulation-অর্থাৎ সব কিছু encapsulate অবস্থায় থাকা। অনেকটা ক্যাপসুল মেডিসিন এর মতোই , যেভাবে একটা capsule এর বাইরে একটা আবরণ দিয়ে সব কিছু ভেতরে আটকানো থাকে । একই ভাবে Object Oriented Programming এ একটা class এর আবরণ দিয়ে সব কিছু ভেতরে আটকানো থাকে।
polymorphism – বহুরূপিতা। অর্থাৎ একটা অবজেক্ট নানা সময় নানা রকম রূপ ধারণ করতে পারে বা বহুরূপে ব্যবহৃত হতে পারে।
Define Class / class কি?
A class is defined by using the class keyword, followed by the name of the class and a pair of curly braces ({}). A class has some properties and method.
Object সম্পর্কে তো জানা হলো , চলুন এবার জানি class কি? class হচ্ছে Object এর replica (অবিকল প্রতিরুপ) বা blueprint (নীলনকশা)। ধরুন আমরা একটি চেয়ার বানাতে চাই, শুরুতে আমরা কোন রকম চিন্তা ভাবনা না করে ফু দিয়ে একটা চেয়ার বানিয়ে ফেলতে পারি না। আমরা এর জন্যে পরিকল্পনা করি- চেয়ারটা দেখতে কেমন হবে, এটি দৈর্ঘ্য, প্রস্থ এবং উচ্চতা কতটুকু হবে, চেয়ারটি কি কি কাজ করবে ইত্যাদি। এই পরিকল্পনা গুলো আমরা আমরা কোথাও লিখে রাখি। আমাদের এই লেখা ডকুমেন্টটি আসলে class. আরেকটু ব্যাখ্যা করে বলতে গেলে আমরা বলতে পারি যে একটা অবজেক্ট কেমন হবে তা যেখানে নির্ধারণ করা থাকে তার নাম class। এই ক্লাস দিয়ে তৈরি অবজেক্টকে ঐ ক্লাসের অস্তিত্ব(Instance) বলে।
<?php
class Fruit {
  // code goes here...
}
?>
Below we declare a class named Fruit consisting of two properties ($name and $color) and two methods set_name() and get_name() for setting and getting the $name property:

<?php
class Fruit {
  // Properties
  public $name;
  public $color;

  // Methods
  function set_name($name) {
    $this->name = $name;
  }
  function get_name() {
    return $this->name;
  }
}
?>
Note: In a class, variables are called properties and functions are called methods!
PHP তে class এর মধ্যে অবস্থিত variable গুলিকে Property বলা হয়। Property গুলোকে “attributes” বা “fields” নামেও ডাকা হয়।PHP তে Property গুলোকে যেকোনো একটা visibility অর্থাৎ : public, private, protected এর যেকোনো একটি visibility সহ ডিফাইন করতে হয়।
Example :
class Person
{
   public $name ="Monir"; // this is called property
}
PHP তে class এর মধ্যে অবস্থিত function গুলিকে Method বলা হয়। PHP তে Method গুলোকে যেকোনো একটা visibility অর্থাৎ : public, private, protected এর যেকোনো একটি visibility সহ declare বা ঘোষণা করতে হয়।

private :Class এর মধ্যে constant, property, method গুলোকে যদি শুধু একই class বা class নিজের মধ্যে ব্যবহার সীমিত রাখতে চাই , তাহলে সেইসব constant, property, method গুলোকে private ঘোষণা করতে হয়।
Example :
class Person
{
   private $name ="Monir"; // this is called property
}
protected: Class এর মধ্যে constant, property, method গুলোকে যদি শুধু একই class বা class নিজের মধ্যে এবং তার child class এর মধ্যে ব্যবহার সীমিত রাখতে চাই , তাহলে সেইসব constant, property, method গুলোকে protected ঘোষণা করতে হয়।
Example :
class Person
{
   protected $name ="Monir"; // this is called property
}
public: Class এর মধ্যে constant, property, method গুলোকে যদি একই class বা class নিজের মধ্যে , child class এবং class এর বাহির থেকে ব্যবহার রাখতে চাই , তাহলে সেইসব constant, property, method গুলোকে public ঘোষণা করতে হয়।

Example :
class Person
{
   public $name ="Monir"; // this is called property
}

static keyword: class এর যেকোনো property এবং method এ কোন রকম instance বা object ছাড়া access করার সুযোগ দিতে চাইলে বা সরাসরি class দিয়ে access করার সুযোগ দিতে চাইলে static keyword ব্যবহৃত হয়।

“::” scope resolution operator PHP তে class এর মধ্যে অবস্থিত যেকোনো static property, static method এবং constant কে class এর ভিতরে অথবা বাহিরে যেকোনো কাজে ব্যবহার করতে চাইলে “:: scope resolution operator” ব্যবহার করতে হয়।
In Object oriented programming . we can crate multiple object from class. Each Object has all the properties and method defined in the class, but they will have different property values.
object of class is created the new keyword.
Example below
$apple and $banana are instances of the class Fruit:
<?php
class Fruit {
  // Properties
  public $name;
  public $color;

  // Methods
  function set_name($name) {
    $this->name = $name;
  }
  function get_name() {
    return $this->name;
  }
}
$apple = new Fruit();
$banana = new Fruit();
$apple->set_name('Apple');
$banana->set_name('Banana');

echo $apple->get_name();
echo "<br>";
echo $banana->get_name();
?>

Define $this Keyword / $this Ki ?

$this keyword refers to the current object, and is only available inside methods.

Example

<?php
class Fruit {
  public $name;
}
$apple = new Fruit();
?>

So, where can we change the value of the $name property? There are two ways:

1. Inside the class (by adding a set_name() method and use $this):

Example

<?php
class Fruit {
  public $name;
  function set_name($name) {
    $this->name = $name;
  }
}
$apple = new Fruit();
$apple->set_name("Apple");
?>
2. Outside the class (by directly change the property value):

Example

<?php
class Fruit {
  public $name;
}
$apple = new Fruit();
$apple->name = "Apple";
?>

PHP - instanceof
You can use the instanceof keyword to check if an object belongs to a specific class:

Example

<?php
$apple = new Fruit();
var_dump($apple instanceof Fruit);
?>

Out Put will be :
bool(true) // its means its belongs to this object.


No comments:

Post a Comment