PHP Classes/Objects

3 min read

A class is a template for objects, and an object is an instance of class.

Ангилал нь обьектуудад зориулсан загвар бөгөөд объект бол ангийн жишээ юм.


OOP Case #

Let’s assume we have a class named Fruit. A Fruit can have properties like name, color, weight, etc. We can define variables like $name, $color, and $weight to hold the values of these properties.

Жимс нэртэй ангитай гэж бодъё. Жимс нь нэр, өнгө, жин гэх мэт шинж чанартай байж болно. Эдгээр шинж чанаруудын утгыг хадгалахын тулд $ name, $ color, $ weight зэрэг хувьсагчуудыг тодорхойлж болно.

When the individual objects (apple, banana, etc.) are created, they inherit all the properties and behaviors from the class, but each object will have different values for the properties.

Бие даасан объектууд (алим, гадил жимсний гэх мэт) -ийг бүтээхдээ тэд бүх шинж чанар, зан үйлийг ангиасаа өвлөн авах боловч объект бүр шинж чанарын хувьд өөр өөр утгатай байх болно.


Define a Class #

A class is defined by using the class keyword, followed by the name of the class and a pair of curly braces ({}). All its properties and methods go inside the braces:

Ангийг class түлхүүр үгийг ашиглан тодорхойлж дараа нь ангийн нэр ба хос буржгар хаалт ({}) оруулна. Түүний бүх шинж чанар, аргууд нь хаалтанд ордог:

Syntax #

<?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:

Доор бид хоёр шинж чанараас ($ name ба $ color) ба set_name () ба get_name () гэсэн хоёр шинж чанараас бүрдэх Fruit нэртэй ангийг зарлаж,

Example #

Жишээ #

<?php class Fruit { // Properties public $name; public $color; // Methods function set_name($name) { $this->name = $name; } function get_name() { return $this->name; } } ?>

Define Objects #

Classes are nothing without objects! We can create multiple objects from a class. Each object has all the properties and methods defined in the class, but they will have different property values.

Ангиуд нь объектгүй бол юу ч биш! Бид ангиас олон объект үүсгэх боломжтой. Объект бүр ангидаа тодорхойлсон бүх шинж чанар, аргуудтай байдаг боловч тэдгээр нь өөр өөр шинж чанартай байх болно.

Objects of a class is created using the new keyword.

Ангийн объектуудыг new түлхүүр үг ашиглан бүтээдэг.

In the example below, $apple and $banana are instances of the class Fruit:

Доорх жишээнд $ алим ба $ гадил жимсний жимсний ангийн жишээ юм.

Example #

Жишээ #

<?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(); ?>

In the example below, we add two more methods to class Fruit, for setting and getting the $color property:

Доорх жишээнд бид Fruit ангилалд $ color шинж чанарыг тохируулах, олж авах өөр хоёр аргыг нэмж оруулав.

Example #

Жишээ #

<?php class Fruit { // Properties public $name; public $color; // Methods function set_name($name) { $this->name = $name; } function get_name() { return $this->name; } function set_color($color) { $this->color = $color; } function get_color() { return $this->color; } } $apple = new Fruit(); $apple->set_name('Apple'); $apple->set_color('Red'); echo "Name: " . $apple->get_name(); echo "<br>"; echo "Color: " . $apple->get_color(); ?>

PHP – The $this Keyword #

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

$ This гэсэн түлхүүр үг нь тухайн объектыг хэлдэг бөгөөд зөвхөн дотор нь ашиглах боломжтой байдаг.

Look at the following example:

Дараах жишээг харна уу:

Example #

Жишээ #

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

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

Тэгэхээр бид $ name үл хөдлөх хөрөнгийн үнэ цэнийг хаанаас өөрчилж болох вэ? Хоёр арга бий:

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

1. Анги дотор (set_name () аргыг нэмж оруулаад $ 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 changing the property value):

2. Ангийн гадна (үл хөдлөх хөрөнгийн утгыг шууд өөрчлөх замаар):

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:

Тухайн объект тодорхой ангид хамааралтай эсэхийг шалгахын тулд instanceof түлхүүр үгийг ашиглаж болно.

Example #

Жишээ #

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

Powered by BetterDocs

Leave a Reply