Chapterبەش 6 · Lectureوانە 6.1
Classes and Objects in PHPکلاسەکان و ئۆبجێکتەکان لە PHPـدا
Why object-oriented PHP, and how to write it: classes, properties and methods, new, the arrow -> (the same arrow as $conn->query() in the MySQL lessons), $this, constructors and PHP 8 constructor property promotion, typed properties, public, private and protected, readonly (PHP 8.1), static members and ::, class constants, a bazaar Product and Cart worked example, objects as shared handles and clone, and common mistakes with PHP's real messages. Every example runs on PHP 8.2 and works the same on 8.3, 8.4 and 8.5.
بۆچی PHPـی تەنخواز و چۆن دەنووسرێت: ، و ، new، تیری -> (هەمان تیری $conn->query() لە وانەکانی MySQL)، $this، و PHP 8، جۆردار، public، private و protected، readonly (PHP 8.1)، و ::، ، نموونەیەکی کارپێکراوی Product و Cartـی بازاڕ، وەک دەسکی هاوبەش و clone و هەڵە باوەکان بە پەیامە ڕاستەقینەکانی PHP. هەموو نموونەیەک لەسەر PHP 8.2 کار دەکات و لەسەر 8.3، 8.4 و 8.5 هەمان شتە.
What you will learnچی فێر دەبیت
- Write a class with typed properties and methods, and make objects from it with new.
- Use -> and $this correctly, and explain the arrow you already used with $conn->query().
- Write a constructor, in the long form and with PHP 8 promotion.
- Choose public, private or readonly, and use static members and class constants with ::.
- Read PHP's error messages about classes and objects and fix the code.
- بە جۆردار و بنووسیت و بە new لێ دروست بکەیت.
- -> و $this بە دروستی بەکار بهێنیت و ئەو تیرە ڕوون بکەیتەوە کە پێشتر لەگەڵ $conn->query() بەکارت هێنا.
- بنووسیت، بە شێوە درێژەکە و بە PHP 8.
- public، private یان readonly هەڵبژێریت و و بە :: بەکار بهێنیت.
- پەیامەکانی هەڵەی PHP لەبارەی و بخوێنیتەوە و کۆدەکە چاک بکەیت.
Why objects in PHP?بۆچی ئۆبجێکتەکان لە PHPـدا؟
Until now your data lived in variables and arrays, and functions worked on it from outside. This style is called , and it is fine for a small script. A real shop site is different: hundreds of products, carts and orders. Soon you have many arrays and many functions, and nothing says which function belongs to which array. A typing mistake in a key, such as $tea["prise"], only gives a warning when the page runs.
تا ئێستا تۆ لە و بوو و لە دەرەوە کاریان لەسەر دەکرد. ئەم شێوازە پێی دەڵێن و بۆ بچووک باشە. ماڵپەڕی دووکانێکی ڕاستەقینە جیاوازە: سەدان بەرهەم، سەبەتە و داواکاری. زوو دەبینیت و زۆرت هەیە و هیچ شتێک نالێت کام هی کام . هەڵەیەکی نووسین لە ، وەک $tea["prise"]، تەنیا لە کاتی کارکردنی لاپەڕەکەدا دەدات.
(OOP) puts the data and the functions that use it together in one unit: an . You have already used objects: in Connecting PHP to MySQL, $conn->query($sql) called a method of a mysqli object. In this lesson you write your own. The big ideas are the same in every language and are also taught in Python and Java; here we focus on how PHP writes them.
(OOP) و ئەو بەکاری دەهێنن لە یەک یەکەدا کۆ دەکاتەوە: . پێشتر بەکار هێناوە: لە بەستنەوەی PHP بە MySQL، $conn->query($sql) mysqli بانگ کرد. لەم وانەیەدا خۆت دەنووسیت. بیرۆکە گەورەکان لە هەموو زمانێکدا وەک یەکن و لە Python و Javaـیشدا دەخوێندرێن؛ لێرەدا سەرنج دەدەینە سەر ئەوەی PHP چۆنیان دەنووسێت.
Class: the plan; object: a thing made from itکلاس: نەخشەکە؛ ئۆبجێکت: شتێک کە لێی دروست دەکرێت
A is a plan. It lists the (the data) that each object will have and the (the functions) it can run. An is one thing built from that plan, with its own values. Making an object from a class is also called making an of it. One Product class can give you as many product objects as the shop needs: tea, rice, honey…
نەخشەیەکە. ئەو () ڕیز دەکات کە هەر دەیبێت و ئەو () کە دەتوانێت جێبەجێیان بکات. یەک شتە کە لەو نەخشەیە دروست کراوە و خۆی هەیە. دروستکردنی لە پێی دەگوترێت دروستکردنی ئەو . یەک Product دەتوانێت هەر چەند بەرهەمی پێویستی دووکانەکە بێت بدات: چا، برنج، هەنگوین…
A bazaar shopkeeper buys a pad of printed price tags: Name: ____ Price: ____ IQD. The printed pad is the class: every tag has the same two blanks. Each filled tag stuck on a jar (Tea, 5,000) is an object: same shape, its own values.
دووکاندارێکی بازاڕ دەفتەرێک کاغەزی نرخی چاپکراو دەکڕێت: ناو: ____ نرخ: ____ دینار. دەفتەرە چاپکراوەکە : هەموو کاغەزێک هەمان دوو بۆشایی هەیە. هەر کاغەزێکی پڕکراوە کە بە قوتووێکەوە نووسێنراوە (چا، 5,000) : هەمان شێوە، بەڵام خۆی.
Where the analogy stops workingسنووری ئەم نموونەیە
A paper tag cannot do anything, but an object also has methods: it can, for example, print its own label.
کاغەزێک ناتوانێت هیچ کارێک بکات، بەڵام هەیە: بۆ نموونە دەتوانێت لەیبڵی خۆی چاپ بکات.
Writing a class and making an objectنووسینی کلاسێک و دروستکردنی ئۆبجێکتێک
Write class, a name and a pair of braces. Inside the braces go the properties (variables with a visibility word such as public, an optional type and a default value) and the methods (functions). The class alone does nothing: new makes an object from it.
class، ناوێک و جووتێک کەوانەی { } بنووسە. لە ناو کەوانەکاندا دێن ( بە وەک public، جۆرێکی ئارەزوومەندانە و ) لەگەڵ (). خودی هیچ ناکات: new لێ دروست دەکات.
<?php
class Product
{
public string $name = "";
public int $price = 0;
public function label(): string
{
return $this->name . ": " . number_format($this->price) . " IQD";
}
}
$tea = new Product(); // make an object from the class
$tea->name = "Tea"; // set its properties
$tea->price = 5000;
echo $tea->label(), "\n"; // call a method
echo $tea->price, "\n"; // read a propertyTea: 5,000 IQD 5000
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
classand a name. Class names are written in PascalCase: every word starts with a capital letter (Product,ShoppingCart).classو ناوێک. ناوی بە PascalCase دەنووسرێن: هەموو وشەیەک بە پیتی گەورە دەست پێ دەکات (Product،ShoppingCart).- Lineدێڕی 4-5
Two properties: a visibility word (
public), a type (string,int), the name with$and a default value.دوو : (
public)، جۆرێک (string،int)، ناوەکە بە$و .- Lineدێڕی 7-10
A method is a function written inside the class.
$thismeans "this object"; the next screen explains it.کە لە ناو نووسراوە.
$thisواتە «ئەم »؛ شاشەی داهاتوو ڕوونی دەکاتەوە.- Lineدێڕی 13
new Product()makes an object;$teaholds it.new Product()دروست دەکات؛$teaهەڵیدەگرێت.- Lineدێڕی 14-15
The arrow
->reaches a property of the object to set it.تیری
->دەگاتە بۆ ئەوەی بداتێ.- Lineدێڕی 17-18
With brackets
->label()calls a method; without them->pricereads a property.بە کەوانەوە
->label()بانگ دەکات؛ بەبێ کەوانە->priceدەخوێنێتەوە.
Writing a class and making an objectنووسینی کلاسێک و دروستکردنی ئۆبجێکتێک
The arrow ->: reaching inside an objectتیری ->: چوونە ناو ئۆبجێکتێک
-> is the . $tea->price reads a property (no brackets), and $tea->label() calls a method (with brackets). After the arrow the property name has no $. It is the same arrow you met in The object operator ->: $conn->query($sql) calls the query() method of a mysqli object, and $result->num_rows reads a property of a result object.
-> . $tea->price دەخوێنێتەوە (بێ کەوانە) و $tea->label() بانگ دەکات (بە کەوانەوە). لە دوای تیرەکە ناوی $ـی نییە. ئەمە هەمان تیرە کە لە کارپێکەری ئۆبجێکت ->دا بینیت: $conn->query($sql) query()ـی mysqli بانگ دەکات و $result->num_rows ئەنجام دەخوێنێتەوە.
A $ after the arrow is a common slip. PHP then takes the value of the variable $price as the property name (an advanced feature called variable property names). Here $price does not exist, so you get two warnings and no price:
$ لە دوای تیرەکە هەڵەیەکی باوە. ئەوکات PHP $price وەک ناوی بەکار دەهێنێت (توانایەکی پێشکەوتووی PHPـە: ناوی لە ). لێرەدا $price بوونی نییە، بۆیە دوو وەردەگریت و هیچ نرخێک چاپ ناکرێت:
<?php
class Product
{
public string $name = "Tea";
public int $price = 5000;
}
$tea = new Product();
echo $tea->$price, "\n"; // wrong: a $ after the arrowWarning: Undefined variable $price in C:\xampp\htdocs\shop\arrow_dollar.php on line 9 Warning: Undefined property: Product::$ in C:\xampp\htdocs\shop\arrow_dollar.php on line 9
$this: the object the method works on$this: ئەو ئۆبجێکتەی میتۆدەکە کاری لەسەر دەکات
A method is written once, in the class, but it runs for many objects. Inside the method, $this is the object the method was called on: in $tea->label() it is $tea, and in $honey->label() it is $honey. So $this->name means "the name of this object". Python writes self for the same idea (Python classes), and Java writes this.
تەنیا یەک جار لە دەنووسرێت، بەڵام بۆ چەندین جێبەجێ دەبێت. لە ناو ، $this ئەو کە لەسەر بانگ کراوە: لە $tea->label()ـدا $teaـیە و لە $honey->label()ـدا $honeyـیە. بۆیە $this->name واتە «ناوی ئەم ». Python بۆ هەمان بیرۆکە self دەنووسێت (کلاسەکانی Python) و Java this دەنووسێت.
<?php
class Product
{
public string $name = "";
public int $price = 0;
public function label(): string
{
return $this->name . ": " . number_format($this->price) . " IQD";
}
}
$tea = new Product();
$tea->name = "Tea";
$tea->price = 5000;
$honey = new Product();
$honey->name = "Honey";
$honey->price = 15000;
echo $tea->label(), "\n"; // inside label(), $this is $tea
echo $honey->label(), "\n"; // inside label(), $this is $honeyTea: 5,000 IQD Honey: 15,000 IQD
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 9
$thisis whichever object stands before the arrow in the call.$thisهەر ئەو کە لە بانگکردنەکەدا پێش تیرەکە هاتووە.- Lineدێڕی 21-22
The same
label()gives two different results.هەمان
label()دوو ئەنجامی جیاواز دەدات.


$this: the object the method works on$this: ئەو ئۆبجێکتەی میتۆدەکە کاری لەسەر دەکات
Common mistake: forgetting $this->هەڵەی باو: لەبیرکردنی $this->
Inside a method, $name alone is a new of that method, not the property. PHP does not look inside the object by itself, so the method below prints two warnings and an empty label:
لە ناو ، $name بە تەنیا نوێی ئەو ، نەک . PHP خۆی ناچێتە ناو ، بۆیە ئەم خوارەوە دوو و لەیبڵێکی بەتاڵ چاپ دەکات:
<?php
class Product
{
public string $name = "Tea";
public int $price = 5000;
public function label(): string
{
return $name . ": " . $price . " IQD"; // $this-> is missing
}
}
$tea = new Product();
echo $tea->label(), "\n";Warning: Undefined variable $name in C:\xampp\htdocs\shop\forgot_this.php on line 9 Warning: Undefined variable $price in C:\xampp\htdocs\shop\forgot_this.php on line 9 : IQD
The fix is to say whose name you mean: return $this->name . ": " . $this->price . " IQD";.
چارەسەرەکە ئەوەیە بڵێیت ناوی کێت مەبەستە: return $this->name . ": " . $this->price . " IQD";.
Constructors: __construct()دروستکەرەکان: __construct()
Setting every property by hand after new is slow and easy to forget. A is a method named __construct that PHP runs by itself each time new makes an object. The arguments you write in new Product("Tea", 5000) go to its parameters. Methods whose names start with two underscores are PHP's magic methods (): PHP calls them for you at the right moment.
دانانی هەموو بە دەست دوای new هێواشە و ئاسان لەبیر دەچێت. بە ناوی __construct کە PHP خۆی هەر جارێک new دروست بکات جێبەجێی دەکات. ئەو لە new Product("Tea", 5000)ـدا دەینووسیت دەچنە . ئەو ناویان بە دوو هێڵی ژێرەوە دەست پێ دەکات PHPـن: PHP خۆی لە کاتی گونجاودا بانگیان دەکات.
<?php
class Product
{
public string $name;
public int $price;
public function __construct(string $name, int $price)
{
$this->name = $name; // property = parameter
$this->price = $price;
echo "Made: $name\n";
}
public function label(): string
{
return $this->name . ": " . number_format($this->price) . " IQD";
}
}
$tea = new Product("Tea", 5000);
$rice = new Product("Rice 5 kg", 12000);
echo $tea->label(), "\n";
echo $rice->label(), "\n";Made: Tea Made: Rice 5 kg Tea: 5,000 IQD Rice 5 kg: 12,000 IQD
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4-5
Typed properties without a default value: the constructor gives them their values.
بە جۆر و بێ : پێ دەدات.
- Lineدێڕی 7-12
$this->nameis the property;$nameis the parameter.$this->name؛$name.- Lineدێڕی 20-21
Each
newruns the constructor once: "Made: …" is printed twice.هەر
newـێک یەک جار جێبەجێ دەکات: «Made: …» دوو جار چاپ دەکرێت.
Constructors: __construct()دروستکەرەکان: __construct()
Constructor property promotion (PHP 8.0)بەرزکردنەوەی پارامیتەر بۆ تایبەتمەندی (PHP 8.0)
In the last example every property is written three times: in the declaration, as a parameter and in an assignment. Since PHP 8.0 a constructor parameter that starts with a visibility word (public, protected or private, also readonly) is promoted: PHP declares the property and fills it with the argument. The body may stay empty, {}; code you write there runs after the properties are filled.
لە نموونەی پێشوودا هەموو سێ جار نووسراوە: لە ناساندندا، وەک و لە دانانی . لە PHP 8.0ـەوە کە بە دەست پێ دەکات (public، protected یان private، هەروەها readonly) بەرز دەکرێتەوە: PHP دەناسێنێت و بە پڕی دەکاتەوە. جەستەکە دەتوانێت بەتاڵ بمێنێتەوە، {}؛ ئەو کۆدەی تێیدا دەینووسیت دوای پڕکردنەوەی جێبەجێ دەبێت.
<?php
class Product
{
public function __construct(
public string $name,
public int $price,
) {}
public function label(): string
{
return $this->name . ": " . number_format($this->price) . " IQD";
}
}
$rice = new Product("Rice 5 kg", 12000);
echo $rice->label(), "\n";
echo $rice->name, "\n";Rice 5 kg: 12,000 IQD Rice 5 kg
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4-7
Each
publicparameter becomes a property. The comma after the last parameter is allowed (PHP 8.0).هەر
publicدەبێتە . فاریزە لە دوای دوایین ڕێگەپێدراوە (PHP 8.0).- Lineدێڕی 15-16
The object works exactly like the long version.
ڕێک وەک وەشانە درێژەکە کار دەکات.
Types on properties and parametersجۆر لەسەر تایبەتمەندییەکان و پارامیتەرەکان
int, string, float, bool, array or a class name written before a property or a parameter is a . PHP checks it while the script runs (typed properties exist since PHP 7.4). What happens to a value that does not fit?
int، string، float، bool، array یان ناوی کە لە پێش یان دەنووسرێت . PHP لە کاتی کارکردنی دەیپشکنێت ( جۆردار لە PHP 7.4ـەوە هەن). ئەو نەگونجێت چی بەسەر دێت؟
<?php
class Product
{
public function __construct(
public string $name,
public int $price,
) {}
}
$a = new Product("Tea", "5000"); // a string that looks like a whole number
var_dump($a->price);
$b = new Product("Tea", "five thousand");int(5000)
Fatal error: Uncaught TypeError: Product::__construct(): Argument #2 ($price) must be of type int, string given, called in C:\xampp\htdocs\shop\types.php on line 13 and defined in C:\xampp\htdocs\shop\types.php:4
Stack trace:
#0 C:\xampp\htdocs\shop\types.php(13): Product->__construct('Tea', 'five thousand')
#1 {main}
thrown in C:\xampp\htdocs\shop\types.php on line 4Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 10-11
"5000" looks like a whole number, so PHP converts it to the int 5000.
"5000" وەک دەچێت، بۆیە PHP دەیگۆڕێت بۆ int 5000.
- Lineدێڕی 13
"five thousand" cannot become an int: a
TypeErrorstops the script."five thousand" ناتوانێت ببێتە int:
TypeErrorـێک ڕادەگرێت.
By default PHP is forgiving and converts values that clearly fit (the same type juggling as in Data types). With declare(strict_types=1); as the first statement of the file, PHP stops converting, and even "5000" is refused:
بە شێوەی بنەڕەتی PHP لێبووردەیە و ئەو دەگۆڕێت کە بە ڕوونی دەگونجێن (هەمان گۆڕینی خۆکاری جۆر کە لە جۆرەکانی دراوەدا بینیت). بە declare(strict_types=1); وەک یەکەم دێڕی ، PHP واز لە گۆڕین دەهێنێت و تەنانەت "5000"ـیش ڕەت دەکرێتەوە:
<?php
declare(strict_types=1);
class Product
{
public function __construct(
public string $name,
public int $price,
) {}
}
$a = new Product("Tea", "5000");Fatal error: Uncaught TypeError: Product::__construct(): Argument #2 ($price) must be of type int, string given, called in C:\xampp\htdocs\shop\types_strict.php on line 12 and defined in C:\xampp\htdocs\shop\types_strict.php:6
Stack trace:
#0 C:\xampp\htdocs\shop\types_strict.php(12): Product->__construct('Tea', '5000')
#1 {main}
thrown in C:\xampp\htdocs\shop\types_strict.php on line 6public, private and protectedpublic، private و protected
Every property, method and constant has a visibility, also called an . public: any code may use it. private: only code inside the same class. protected: the class and its child classes (next lesson). A method or constant written without one is public, but always write the word yourself.
هەموو ، و هەیە. public: هەموو کۆدێک دەتوانێت بەکاری بهێنێت. private: تەنیا کۆدی ناو هەمان . protected: و (وانەی داهاتوو). یان کە بەبێ ئەم وشەیە نووسرابێت publicـە، بەڵام هەمیشە خۆت وشەکە بنووسە.
Good classes keep their data private and let public methods guard it. This is : below, nobody can set a price of zero or less, because the only way in is setPrice(), which checks the value. The same idea in other languages: Java and Python.
باش خۆیان private دەهێڵنەوە و ڕێگە دەدەن public پاسەوانی بکەن. ئەمە : لە خوارەوە کەس ناتوانێت نرخی سفر یان کەمتر دابنێت، چونکە تاکە ڕێگەی چوونەژوورەوە setPrice()ـە کە دەپشکنێت. هەمان بیرۆکە لە زمانەکانی تردا: Java و Python.
<?php
class Product
{
public function __construct(
public string $name,
private int $price,
) {}
public function price(): int // a getter: read the price
{
return $this->price;
}
public function setPrice(int $newPrice): bool
{
if ($newPrice <= 0) {
return false; // refuse: the price stays as it was
}
$this->price = $newPrice;
return true;
}
}
$tea = new Product("Tea", 5000);
var_dump($tea->setPrice(-100)); // refused
var_dump($tea->setPrice(6000)); // accepted
echo $tea->price(), "\n";bool(false) bool(true) 6000
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 6
private: only Product's own methods may read or change$price.private: تەنیا خودی Product دەتوانن$priceبخوێننەوە یان بیگۆڕن.- Lineدێڕی 9-12
A : the public way to read the price.
: ڕێگە
publicـەکە بۆ خوێندنەوەی نرخەکە.- Lineدێڕی 14-21
A that refuses bad values.
کە خراپ ڕەت دەکاتەوە.
public, private and protectedpublic، private و protected
Common mistake: using private members from outsideهەڵەی باو: بەکارهێنانی بەشە privateـەکان لە دەرەوە
Writing a private property or calling a private method from outside the class stops the script. Read the message: it names the class and the member.
نووسینی private یان بانگکردنی private لە دەرەوەی ڕادەگرێت. پەیامەکە بخوێنەرەوە: ناوی و بەشەکە دەڵێت.
<?php
class Product
{
public function __construct(
public string $name,
private int $price,
) {}
}
$tea = new Product("Tea", 5000);
$tea->price = -100; // from outside the classFatal error: Uncaught Error: Cannot access private property Product::$price in C:\xampp\htdocs\shop\private_property.php:11
Stack trace:
#0 {main}
thrown in C:\xampp\htdocs\shop\private_property.php on line 11<?php
class Product
{
public function __construct(
public string $name,
private int $price,
) {}
private function costPrice(): int // the shop's secret
{
return intdiv($this->price * 80, 100);
}
}
$tea = new Product("Tea", 5000);
echo $tea->costPrice();Fatal error: Uncaught Error: Call to private method Product::costPrice() from global scope in C:\xampp\htdocs\shop\private_method.php:16
Stack trace:
#0 {main}
thrown in C:\xampp\htdocs\shop\private_method.php on line 16The fix is not to make everything public. Use the public methods the class offers (price(), setPrice()), or add a new public method that does the job safely.
چارەسەرەکە ئەوە نییە هەموو شتێک بکەیتە public. publicـی بەکار بهێنە (price()، setPrice()) یان publicـی نوێ زیاد بکە کە کارەکە بە سەلامەتی دەکات.
readonly: set once, never changed (PHP 8.1)readonly: یەک جار دادەنرێت، هەرگیز ناگۆڕێت (PHP 8.1)
Some values must never change after the object exists: a product code, an order number. A (PHP 8.1) can be written exactly once, from inside its class, usually by the constructor. It must have a type and cannot have a default value. A normal property beside it can still change.
هەندێک نابێت دوای دروستبوونی بگۆڕێن: کۆدی بەرهەم، ژمارەی داواکاری. (PHP 8.1) تەنیا یەک جار دەنووسرێت، لە ناو خۆیەوە، زۆرجار لەلایەن . دەبێت جۆری هەبێت و ناتوانێت هەبێت. ئاسایی لە تەنیشتیدا هێشتا دەتوانێت بگۆڕێت.
<?php
class Product
{
public function __construct(
public readonly string $code,
public string $name,
) {}
}
$tea = new Product("TEA-001", "Tea");
$tea->name = "Black tea"; // fine: name is not readonly
echo $tea->code, " ", $tea->name, "\n";
$tea->code = "TEA-002"; // not allowedTEA-001 Black tea
Fatal error: Uncaught Error: Cannot modify readonly property Product::$code in C:\xampp\htdocs\shop\readonly.php:13
Stack trace:
#0 {main}
thrown in C:\xampp\htdocs\shop\readonly.php on line 13Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 5
readonly: written once, by the constructor.readonly: یەک جار دەنووسرێت، لەلایەن .- Lineدێڕی 11
nameis a normal property: changing it is fine.nameئاساییە: گۆڕینی کێشە نییە.- Lineدێڕی 13
A second write to
codestops the script.نووسینی دووەمی
codeڕادەگرێت.
static: members that belong to the classstatic: ئەو بەشانەی هی کلاسەکەن
A normal property belongs to each object. A static property belongs to the class itself: there is one copy, shared by every object. You reach it with ::, the : Product::$count outside the class, self::$count inside it. A static method, such as Product::count(), needs no object, so there is no $this inside it. These are ; Java has the same idea (static members).
ئاسایی هی هەر . static هی خودی : یەک کۆپی هەیە کە هەموو هاوبەشی دەکەن. بە :: پێی دەگەیت، کە : Product::$count لە دەرەوەی و self::$count لە ناوەوەی. static، وەک Product::count()، پێویستی بە نییە، بۆیە $this لە ناویدا نییە. ئەمانە ؛ Javaـش هەمان بیرۆکەی هەیە (ئەندامە ستاتیکەکان).
<?php
class Product
{
public const CURRENCY = "IQD"; // a class constant
private static int $count = 0; // one counter for the whole class
public function __construct(
public string $name,
public int $price,
) {
self::$count++; // :: reaches the class itself
}
public static function count(): int // a static method
{
return self::$count;
}
public function label(): string
{
return $this->name . ": " . number_format($this->price) . " " . self::CURRENCY;
}
}
echo Product::count(), " products\n";
$tea = new Product("Tea", 5000);
$rice = new Product("Rice 5 kg", 12000);
echo Product::count(), " products\n";
echo $rice->label(), "\n";
echo Product::CURRENCY, "\n";0 products 2 products Rice 5 kg: 12,000 IQD IQD
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 4
A : a fixed value of the class.
: جێگیری .
- Lineدێڕی 5
One counter for the whole class, not one per object.
یەک ژمێرەر بۆ هەموو ، نەک یەک بۆ هەر .
- Lineدێڕی 11
Every constructor call adds one to the shared counter.
هەر بانگکردنێکی یەک بۆ ژمێرەرە هاوبەشەکە زیاد دەکات.
- Lineدێڕی 25
Product::count()works before any object exists.Product::count()پێش بوونی هیچ کار دەکات.
static: members that belong to the classstatic: ئەو بەشانەی هی کلاسەکەن
Class constants, and -> or ::نەگۆڕەکانی کلاس و هەڵبژاردنی -> یان ::
public const CURRENCY = "IQD"; is a : a fixed value that belongs to the class. It has no $, is usually written in capitals and is read with Product::CURRENCY outside the class or self::CURRENCY inside it. (Constants outside classes are in Constants and scope.) PHP 8.3 also allows a type on a class constant, public const string CURRENCY = "IQD";, which PHP 8.2 does not accept.
public const CURRENCY = "IQD"; : جێگیرە کە هی . $ـی نییە، زۆرجار بە پیتی گەورە دەنووسرێت و بە Product::CURRENCY لە دەرەوەی یان self::CURRENCY لە ناوەوەی دەخوێندرێتەوە. ( لە دەرەوەی لە نەگۆڕەکان و مەودادان.) PHP 8.3 ڕێگە بە جۆریش لەسەر دەدات، public const string CURRENCY = "IQD";، کە PHP 8.2 قبووڵی ناکات.
A static method has no object, so $this inside it is an error:
static هیچ نییە، بۆیە $this لە ناویدا هەڵەیە:
<?php
class Product
{
public string $name = "Tea";
public static function shout(): string
{
return strtoupper($this->name); // no object here
}
}
echo Product::shout();Fatal error: Uncaught Error: Using $this when not in object context in C:\xampp\htdocs\shop\static_this.php:8
Stack trace:
#0 C:\xampp\htdocs\shop\static_this.php(12): Product::shout()
#1 {main}
thrown in C:\xampp\htdocs\shop\static_this.php on line 8Worked example: the bazaar cartنموونەی کارپێکراو: سەبەتەی بازاڕ
The task. A small bazaar shop wants a page that prints a receipt: every product with its quantity and cost, then the total. The plan. Two classes. Product keeps a name that never changes (readonly), a private price with a getter, the constant CURRENCY and a static helper money() that formats an amount. Cart keeps a private array of lines and has three methods: add(), total() and receipt(). A cart has products; it is not a kind of product, so it holds Product objects instead of copying their code ().
ئەرکەکە. دووکانێکی بچووکی بازاڕ لاپەڕەیەکی دەوێت پسوولەیەک چاپ بکات: هەموو بەرهەمێک بە ژمارە و تێچووەکەیەوە، پاشان کۆی گشتی. پلانەکە. دوو . Product ناوێک هەڵدەگرێت کە هەرگیز ناگۆڕێت (readonly)، نرخێکی private بە ، CURRENCY و یارمەتیدەری static بە ناوی money() کە بڕێک پارە ڕێک دەخات. Cart privateـی دێڕەکان هەڵدەگرێت و سێ هەیە: add()، total() و receipt(). سەبەتە بەرهەمی تێدایە؛ جۆرێک لە بەرهەم نییە، بۆیە Product هەڵدەگرێت لەبری ئەوەی کۆدەکەیان کۆپی بکات ().
<?php
class Product
{
public const CURRENCY = "IQD";
public function __construct(
public readonly string $name,
private int $price,
) {}
public function price(): int
{
return $this->price;
}
public static function money(int $amount): string
{
return number_format($amount) . " " . self::CURRENCY;
}
}
class Cart
{
private array $lines = []; // each line: a product and a quantity
public function add(Product $product, int $qty = 1): void
{
$this->lines[] = ["product" => $product, "qty" => $qty];
}
public function total(): int
{
$sum = 0;
foreach ($this->lines as $line) {
$sum += $line["product"]->price() * $line["qty"];
}
return $sum;
}
public function receipt(): string
{
$text = "";
foreach ($this->lines as $line) {
$p = $line["product"];
$cost = $p->price() * $line["qty"];
$text .= sprintf("%-12s x%d %14s\n", $p->name, $line["qty"], Product::money($cost));
}
return $text . sprintf("%-15s %14s\n", "Total", Product::money($this->total()));
}
}
$tea = new Product("Tea", 5000);
$rice = new Product("Rice 5 kg", 12000);
$honey = new Product("Honey", 15000);
$cart = new Cart();
$cart->add($tea, 2);
$cart->add($rice);
$cart->add($honey);
echo $cart->receipt();Tea x2 10,000 IQD Rice 5 kg x1 12,000 IQD Honey x1 15,000 IQD Total 37,000 IQD
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 7-8
The name can be read by anyone but never changed; the price is private.
ناوەکە هەموو کەس دەیخوێنێتەوە، بەڵام هەرگیز ناگۆڕێت؛ نرخەکە privateـە.
- Lineدێڕی 16-19
A static helper:
Product::money(37000)gives "37,000 IQD" without any object.یارمەتیدەرێکی static:
Product::money(37000)بێ هیچ "37,000 IQD" دەدات.- Lineدێڕی 24
Only Cart's own methods can change the lines.
تەنیا خودی Cart دەتوانن دێڕەکان بگۆڕن.
- Lineدێڕی 26
Product $product: the type declaration accepts only Product objects;$qtyhas the default value 1.Product $product: تەنیا Product وەردەگرێت؛$qty1ـی هەیە.- Lineدێڕی 35
The cart asks each product for its price with
->price(); it never touches the private property.سەبەتەکە بە
->price()نرخی هەر بەرهەمێک دەپرسێت؛ هەرگیز دەست لە privateـەکە نادات.- Lineدێڕی 46
sprintflines the columns up:%-12spads the name to 12 characters.sprintfستوونەکان ڕێک دەخات:%-12sناوەکە دەگەیەنێتە 12 پیت.


Worked example: the bazaar cartنموونەی کارپێکراو: سەبەتەی بازاڕ
Objects are shared, not copiedئۆبجێکتەکان هاوبەش دەکرێن، کۆپی ناکرێن
$same = $tea; does not copy the object. Both variables now hold a handle to the same object, so a change made through one is seen through the other. Objects passed to a method, as in $cart->add($tea), travel the same way: the cart sees a later price change. Arrays behave differently: they are copied (pass by value). To get a real second object, use clone. Java works the same way (two variables, one object).
$same = $tea; کۆپی ناکات. ئێستا هەردوو دەسکێک بۆ هەمان هەڵدەگرن، بۆیە گۆڕانکارییەک کە لە ڕێگەی یەکێکیانەوە بکرێت لە ڕێگەی ئەوی تریشەوە دەبینرێت. ئەو دەدرێنە ، وەک لە $cart->add($tea)ـدا، بە هەمان شێوە دەڕۆن: سەبەتەکە گۆڕینی دواتری نرخ دەبینێت. جیاوازن: کۆپی دەکرێن (ناردن بە نرخ). بۆ دووەمی ڕاستەقینە clone بەکار بهێنە. Javaـش بە هەمان شێوە کار دەکات (دوو گۆڕاو، یەک ئۆبجێکت).
<?php
class Product
{
public function __construct(
public string $name,
public int $price,
) {}
}
$tea = new Product("Tea", 5000);
$same = $tea; // copies the handle, not the object
$same->price = 6000;
echo $tea->price, "\n"; // 6000: one object, two names
$copy = clone $tea; // a real second object
$copy->price = 7000;
echo $tea->price, "\n"; // still 6000
echo $copy->price, "\n";
var_dump($same === $tea, $copy === $tea);6000 6000 7000 bool(true) bool(false)
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 11-13
$sameand$teaare the same object, so$tea->pricebecomes 6000.$sameو$teaهەمان ، بۆیە$tea->priceدەبێتە 6000.- Lineدێڕی 15-18
clonemakes a new object; changing the copy leaves$teaalone.cloneنوێ دروست دەکات؛ گۆڕینی کۆپییەکە دەست لە$teaنادات.- Lineدێڕی 19
===on objects asks: is it the very same object?===لەسەر دەپرسێت: ئایا ڕێک هەمان ؟
Common mistakesهەڵە باوەکان
Every message below was produced by PHP 8.2. The message names the class and the member, so read it before you change anything.
هەموو پەیامەکانی خوارەوە PHP 8.2 دروستی کردوون. پەیامەکە ناوی و بەشەکە دەڵێت، بۆیە پێش ئەوەی هیچ بگۆڕیت بیخوێنەرەوە.
| Mistakeهەڵە | What PHP saysPHP چی دەڵێت | Fixچاککردن |
|---|---|---|
$tea = Product("Tea", 5000); | Error: Call to undefined function Product() | Write new Product("Tea", 5000).new Product("Tea", 5000) بنووسە. |
function _construct(...) | Error: Typed property Product::$name must not be accessed before initialization | One underscore: the constructor never ran. Write __construct with two.یەک هێڵی ژێرەوە: هەرگیز جێبەجێ نەبوو. __construct بە دوو هێڵ بنووسە. |
echo $tea->label; | Warning: Undefined property: Product::$label | A method needs brackets: $tea->label(). کەوانەی دەوێت: $tea->label(). |
return $name; // in a method | Warning: Undefined variable $name | Use the property: $this->name. بەکار بهێنە: $this->name. |
echo $tea->count; // static | Notice: Accessing static property Product::$count as non static | Static members use ::: Product::$count or a static method. :: بەکار دەهێنن: Product::$count یان static. |
$this in a static method | Error: Using $this when not in object context | Make the method non-static, or use self:: for class members. بکە بە ناستاتیک، یان self:: بۆ بەشەکانی بەکار بهێنە. |
$tea->price = -100; // private | Error: Cannot access private property Product::$price | Use the public method: $tea->setPrice(6000). publicـەکە بەکار بهێنە: $tea->setPrice(6000). |
$tea->code = "TEA-002"; // readonly | Error: Cannot modify readonly property Product::$code | Give the value once, in the constructor; make a new object if it must differ. یەک جار لە بدە؛ ئەگەر دەبێت جیاواز بێت نوێ دروست بکە. |
<?php
class Product
{
public string $name;
public function _construct(string $name) // only ONE underscore
{
$this->name = $name;
}
}
$tea = new Product("Tea");
echo $tea->name;Fatal error: Uncaught Error: Typed property Product::$name must not be accessed before initialization in C:\xampp\htdocs\shop\one_underscore.php:13
Stack trace:
#0 {main}
thrown in C:\xampp\htdocs\shop\one_underscore.php on line 13PHP 8.2 here, the same on 8.3, 8.4 and 8.5PHP 8.2 لێرە، هەمان شت لەسەر 8.3، 8.4 و 8.5
Every example in this lesson ran on PHP 8.2, the version in the course's XAMPP, and works the same on PHP 8.3, 8.4 and 8.5. Newer versions only add features: typed class constants and the #[\Override] check (8.3), property hooks, public private(set) and new Product("Tea", 5000)->label() without extra brackets (8.4), and clone with new values (8.5). As of November 2026, PHP 8.4 and 8.5 are actively supported, and 8.2 receives only security fixes until 31 December 2026, so choose a supported version when you install XAMPP or rent hosting.
هەموو نموونەکانی ئەم وانەیە لەسەر PHP 8.2 جێبەجێ کران، کە وەشانی XAMPPـی خولەکەیە و لەسەر PHP 8.3، 8.4 و 8.5 بە هەمان شێوە کار دەکەن. وەشانە نوێکان تەنیا تایبەتمەندی زیاد دەکەن: جۆر لەسەر و پشکنینی #[\Override] (8.3)، property hooks، public private(set) و new Product("Tea", 5000)->label() بێ کەوانەی زیادە (8.4) و clone بە نوێ (8.5). تا نۆڤەمبەری 2026، PHP 8.4 و 8.5 پشتگیریی چالاکیان هەیە و 8.2 تەنیا تا 31ی دیسەمبەری 2026 چاککردنەوەی ئاسایش وەردەگرێت، بۆیە کاتێک XAMPP دادەمەزرێنیت یان هۆستینگ بەکرێ دەگریت وەشانێکی پشتگیریکراو هەڵبژێرە.
Exercisesڕاهێنانەکان
Write a class Student with a promoted public name and a private array of marks that starts empty. Add addMark(int $mark) and average(): float, which returns 0.0 when there are no marks. Give Lana the marks 78, 91 and 85, print her average with one decimal place, then print the average of Soran, who has no marks.
Student بنووسە بە nameـێکی publicـی بەرزکراوە و privateـی نمرەکان کە بە بەتاڵی دەست پێ دەکات. addMark(int $mark) و average(): float زیاد بکە کە ئەگەر هیچ نمرەیەک نەبێت 0.0 دەگەڕێنێتەوە. نمرەکانی 78، 91 و 85 بدە بە لانا، تێکڕاکەی بە یەک ژمارەی دوای خاڵ چاپ بکە، پاشان تێکڕای سۆران چاپ بکە کە هیچ نمرەیەکی نییە.
Hintئاماژە
count($this->marks) gives the number of marks; check it before you divide.
count($this->marks) ژمارەی نمرەکان دەدات؛ پێش دابەشکردن بیپشکنە.
Show answerوەڵامەکە پیشان بدە
The marks stay private; the class guards the empty case so that it never divides by zero.
نمرەکان privateـن؛ ئاگای لە دۆخی بەتاڵە بۆ ئەوەی هەرگیز دابەشکردن بەسەر سفردا نەکات.
<?php
class Student
{
public function __construct(
public string $name,
private array $marks = [],
) {}
public function addMark(int $mark): void
{
$this->marks[] = $mark;
}
public function average(): float
{
if (count($this->marks) === 0) {
return 0.0;
}
return array_sum($this->marks) / count($this->marks);
}
}
$lana = new Student("Lana");
$lana->addMark(78);
$lana->addMark(91);
$lana->addMark(85);
printf("%s: %.1f\n", $lana->name, $lana->average());
$soran = new Student("Soran");
printf("%s: %.1f\n", $soran->name, $soran->average());Lana: 84.7 Soran: 0.0
This tea-house class has three mistakes. Find them using PHP's messages, then fix them so that the program prints Tea with 3 spoons of sugar.
ئەم چایخانەیە سێ هەڵەی تێدایە. بە یارمەتیی پەیامەکانی PHP بیاندۆزەرەوە، پاشان چاکیان بکە بۆ ئەوەی Tea with 3 spoons of sugar چاپ بکات.
<?php
class TeaGlass
{
private int $sugar = 0;
public function addSugar(int $spoons): void
{
$sugar = $sugar + $spoons;
}
public function describe(): string
{
return "Tea with " . $this->sugar . " spoons of sugar";
}
}
$glass = TeaGlass();
$glass->addSugar(2);
$glass->sugar = 5;
echo $glass->describe();Hintئاماژە
Run it after each fix: PHP reports the first fatal error only.
دوای هەر چاککردنێک جێبەجێی بکە: PHP تەنیا یەکەم ڕادەگەیەنێت.
Show answerوەڵامەکە پیشان بدە
Run 1 stops at line 17, Call to undefined function TeaGlass(): new is missing. After adding it, run 2 shows Warning: Undefined variable $sugar (line 8: $this-> is missing) and then Cannot access private property TeaGlass::$sugar (line 19: outside code may not write a private property). The fixed program adds the sugar through the public method:
جێبەجێکردنی یەکەم لە دێڕی 17 ڕادەوەستێت، Call to undefined function TeaGlass(): new نییە. دوای زیادکردنی، جێبەجێکردنی دووەم Warning: Undefined variable $sugar پیشان دەدات (دێڕی 8: $this-> نییە) و پاشان Cannot access private property TeaGlass::$sugar (دێڕی 19: کۆدی دەرەوە ناتوانێت private بنووسێت). بەرنامە چاککراوەکە شەکرەکە لە ڕێگەی publicـەکەوە زیاد دەکات:
<?php
class TeaGlass
{
private int $sugar = 0;
public function addSugar(int $spoons): void
{
$this->sugar = $this->sugar + $spoons; // fix 1: $this->
}
public function describe(): string
{
return "Tea with " . $this->sugar . " spoons of sugar";
}
}
$glass = new TeaGlass(); // fix 2: new
$glass->addSugar(2);
$glass->addSugar(1); // fix 3: change sugar through a public method
echo $glass->describe(), "\n";Tea with 3 spoons of sugar
Write TaxiRide with readonly from and to, a private km, the constants START_FARE = 2000 and PER_KM = 500, and a static counter of rides. fare() returns the start fare plus 500 dinars per kilometre, rounded to a whole number. Make three rides (4.5 km, 11 km and 12.2 km), print each fare and then the number of rides.
TaxiRide بنووسە بە from و toـی readonly، kmـێکی private، START_FARE = 2000 و PER_KM = 500 و ژمێرەرێکی static بۆ گەشتەکان. fare() کرێی دەستپێک لەگەڵ 500 دینار بۆ هەر کیلۆمەترێک دەگەڕێنێتەوە، بە . سێ گەشت دروست بکە (4.5 km، 11 km و 12.2 km)، کرێی هەریەکەیان و پاشان ژمارەی گەشتەکان چاپ بکە.
Hintئاماژە
(int) round($this->km * self::PER_KM) gives a whole number of dinars.
(int) round($this->km * self::PER_KM) بڕی دینارەکە وەک دەدات.
Show answerوەڵامەکە پیشان بدە
Constants and the counter belong to the class (self::), the distance belongs to each ride ($this->).
و ژمێرەرەکە هی (self::)، دوورییەکە هی هەر گەشتێکە ($this->).
<?php
class TaxiRide
{
public const START_FARE = 2000; // dinars when the ride starts
public const PER_KM = 500; // dinars for every kilometre
private static int $rides = 0;
public function __construct(
public readonly string $from,
public readonly string $to,
private float $km,
) {
self::$rides++;
}
public function fare(): int
{
return self::START_FARE + (int) round($this->km * self::PER_KM);
}
public static function rides(): int
{
return self::$rides;
}
}
$trips = [
new TaxiRide("Bazaar", "University", 4.5),
new TaxiRide("University", "Airport", 11),
new TaxiRide("Airport", "Bazaar", 12.2),
];
foreach ($trips as $t) {
echo "$t->from -> $t->to: ", number_format($t->fare()), " IQD\n";
}
echo TaxiRide::rides(), " rides\n";Bazaar -> University: 4,250 IQD University -> Airport: 7,500 IQD Airport -> Bazaar: 8,100 IQD 3 rides
Add two methods to the cart of the worked example: count(), the number of items (the sum of the quantities), and remove(string $name), which deletes the lines of that product. Test it with 2 × Tea and 1 × Honey, then remove the tea.
دوو بۆ سەبەتەی نموونە کارپێکراوەکە زیاد بکە: count()، ژمارەی کاڵاکان (کۆی ژمارەکان) و remove(string $name) کە دێڕەکانی ئەو بەرهەمە دەسڕێتەوە. بە 2 × Tea و 1 × Honey تاقیی بکەرەوە، پاشان چاکە لاببە.
Hintئاماژە
unset($this->lines[$i]) removes one element of the array inside a foreach ($this->lines as $i => $line).
unset($this->lines[$i]) توخمێکی لە ناو foreach ($this->lines as $i => $line)ـدا لادەبات.
Show answerوەڵامەکە پیشان بدە
Both methods work only on the private $lines, so the rest of the program does not change.
هەردوو تەنیا لەسەر $linesـی private کار دەکەن، بۆیە باقیی بەرنامەکە ناگۆڕێت.
<?php
class Product
{
public function __construct(
public readonly string $name,
private int $price,
) {}
public function price(): int
{
return $this->price;
}
}
class Cart
{
private array $lines = [];
public function add(Product $product, int $qty = 1): void
{
$this->lines[] = ["product" => $product, "qty" => $qty];
}
public function count(): int
{
$items = 0;
foreach ($this->lines as $line) {
$items += $line["qty"];
}
return $items;
}
public function remove(string $name): void
{
foreach ($this->lines as $i => $line) {
if ($line["product"]->name === $name) {
unset($this->lines[$i]);
}
}
}
public function total(): int
{
$sum = 0;
foreach ($this->lines as $line) {
$sum += $line["product"]->price() * $line["qty"];
}
return $sum;
}
}
$cart = new Cart();
$cart->add(new Product("Tea", 5000), 2);
$cart->add(new Product("Honey", 15000));
echo "Items: ", $cart->count(), ", total: ", number_format($cart->total()), " IQD\n";
$cart->remove("Tea");
echo "Items: ", $cart->count(), ", total: ", number_format($cart->total()), " IQD\n";Items: 3, total: 25,000 IQD Items: 1, total: 15,000 IQD
Summaryکورتە
- A class is a plan;
newmakes objects from it, each with its own property values. ->reaches an object's properties ($tea->price, no$after the arrow) and methods ($tea->label(), with brackets). Inside a method,$thisis the object before the arrow.__construct()runs on everynew. Since PHP 8.0,public/privatebefore a constructor parameter declares and fills the property (promotion).- Type declarations (
int $price) are checked while the script runs;declare(strict_types=1);turns off automatic conversion. publicfor everyone,privatefor the class's own code,protectedalso for child classes. Keep data private and guard it with methods (encapsulation).readonly(8.1): written once, then locked.staticmembers and class constants belong to the class and are reached with::(self::inside).- Assigning an object copies its handle, not the object;
clonemakes a copy. - Next: build classes on other classes with inheritance, interfaces and traits in Inheritance, Interfaces and Traits. Later, ORM: Working with Tables as Objects turns database rows into objects like these.
- نەخشەیەکە؛
newلێ دروست دەکات و هەریەکەیان خۆی هەیە. ->دەگاتە ($tea->price، بێ$لە دوای تیرەکە) و ($tea->label()، بە کەوانەوە). لە ناو ،$thisئەو کە پێش تیرەکەیە.__construct()لە هەمووnewـێکدا جێبەجێ دەبێت. لە PHP 8.0ـەوە،public/privateلە پێش دەناسێنێت و پڕی دەکاتەوە ().- (
int $price) لە کاتی کارکردنی دەپشکنرێن؛declare(strict_types=1);گۆڕینی خۆکار ڕادەگرێت. publicبۆ هەمووان،privateبۆ کۆدی خودی ،protectedهەروەها بۆ . private بهێڵەوە و بە بیپارێزە ().readonly(8.1): یەک جار دەنووسرێت و پاشان قفڵ دەدرێت. و هی و بە::پێیان دەگەیت (لە ناوەوەself::).- یەکسانکردنی دەسکەکەی کۆپی دەکات نەک ؛
cloneکۆپییەک دروست دەکات. - دواتر: لەسەر تر بنیات بنێ بە ، و لە بۆماوە، نێوبەستەکان و ترەیتەکان. دواتر، ORM: کارکردن لەگەڵ خشتەکان وەک ئۆبجێکتەکان دەکات بە لەم جۆرە.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Inheritance, Interfaces and Traitsبۆماوە، نێوبەستەکان و ترەیتەکان
Related lecturesوانە پەیوەندیدارەکان
- Inheritance, Interfaces and Traitsبۆماوە، نێوبەستەکان و ترەیتەکان
- Connecting PHP to MySQLپەیوەندیکردنی PHP بە MySQL
- Constants and variable scopeنەگۆڕەکان و مەودای گۆڕاوەکان
- Data typesجۆرەکانی دراوە
- ORM: Working with Tables as ObjectsORM: کارکردن لەگەڵ خشتەکان وەک ئۆبجێکتەکان
- Classes and Objects: Attributes and Methodsکلاسەکان و ئۆبجێکتەکان: تایبەتمەندییەکان و میتۆدەکان
- Constructors, Modifiers and Encapsulationدروستکەرەکان، دیاریکەرەکان و کەپسوولکردن
- Classes and Objectsکلاسەکان و ئۆبجێکتەکان
- Encapsulation and Propertiesکەپسوولکردن و خەسڵەتە بەڕێوەبراوەکان
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- PHP Manual: Classes and Objects (introduction)
- PHP Manual: The Basics (class, new, $this, ->, objects as handles, ::class)
- PHP Manual: Properties (typed properties, readonly properties)
- PHP Manual: Constructors and Destructors (constructor property promotion)
- PHP Manual: Visibility (public, protected, private; asymmetric visibility in 8.4)
- PHP Manual: Static Keyword
- PHP Manual: Class Constants (typed class constants in 8.3)
- PHP Manual: Scope Resolution Operator (::)
- php.net: Supported Versions (checked November 2026)
- php.net: PHP 8.4 release announcement (property hooks, asymmetric visibility, new without parentheses)
- php.net: PHP 8.5 release announcement (clone with)
- PHP The Right Way: Programming paradigms
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.