{"id":123,"date":"2026-05-20T11:05:34","date_gmt":"2026-05-20T11:05:34","guid":{"rendered":"https:\/\/gigz.pk\/php\/?post_type=lesson&#038;p=123"},"modified":"2026-05-21T14:40:21","modified_gmt":"2026-05-21T14:40:21","slug":"classes-and-objects","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/php\/?lesson=classes-and-objects","title":{"rendered":"Classes and Objects"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Introduction<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Classes and Objects are fundamental concepts in Object-Oriented Programming (OOP) in PHP. They help developers organize code into reusable structures, making applications easier to manage, maintain, and scale.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">A class acts as a blueprint, while an object is an instance of that class.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Objectives<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">By the end of this training, you will be able to:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Understand classes and objects in PHP<\/li>\n\n\n\n<li>Create classes using PHP syntax<\/li>\n\n\n\n<li>Define properties and methods<\/li>\n\n\n\n<li>Create and use objects<\/li>\n\n\n\n<li>Access class members<\/li>\n\n\n\n<li>Understand constructors<\/li>\n\n\n\n<li>Build reusable and organized code<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Class<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A class is a template or blueprint used to create objects. It contains variables and functions that define the behavior of the object.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example of a Class<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class Student {<br>    public $name;<br>    public $age;<br>}<br>?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">In this example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Student is the class name<\/li>\n\n\n\n<li>$name and $age are properties<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">What is an Object<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">An object is an instance of a class. Objects use the structure defined in the class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Creating an Object<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class Student {<br>    public $name;<br>    public $age;<br>}<br><br>$student1 = new Student();<br>?&gt;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Here:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>new Student() creates an object<\/li>\n\n\n\n<li>$student1 is the object variable<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Accessing Properties<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">You can assign and display values using the object operator.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class Student {<br>    public $name;<br>    public $age;<br>}<br><br>$student1 = new Student();<br><br>$student1-&gt;name = \"Ali\";<br>$student1-&gt;age = 20;<br><br>echo $student1-&gt;name;<br>echo $student1-&gt;age;<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Methods in a Class<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Methods are functions inside a class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example of a Method<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class Student {<br><br>    public function message() {<br>        echo \"Welcome to PHP OOP\";<br>    }<br>}<br><br>$student1 = new Student();<br>$student1-&gt;message();<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using Properties and Methods Together<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class Car {<br><br>    public $brand;<br><br>    public function showBrand() {<br>        echo $this-&gt;brand;<br>    }<br>}<br><br>$car1 = new Car();<br><br>$car1-&gt;brand = \"Toyota\";<br>$car1-&gt;showBrand();<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Understanding the $this Keyword<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The $this keyword refers to the current object inside the class.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">It is used to access properties and methods within the same class.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class Employee {<br><br>    public $name;<br><br>    public function setName($name) {<br>        $this-&gt;name = $name;<br>    }<br><br>    public function getName() {<br>        return $this-&gt;name;<br>    }<br>}<br><br>$emp = new Employee();<br><br>$emp-&gt;setName(\"Ahmed\");<br><br>echo $emp-&gt;getName();<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Constructors in PHP<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A constructor is a special method that automatically runs when an object is created.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Constructor Example<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php<br>class Mobile {<br><br>    public $brand;<br><br>    public function __construct($brand) {<br>        $this-&gt;brand = $brand;<br>    }<br><br>    public function display() {<br>        echo $this-&gt;brand;<br>    }<br>}<br><br>$phone = new Mobile(\"Samsung\");<br>$phone-&gt;display();<br>?&gt;<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Advantages of Classes and Objects<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Organizes code efficiently<\/li>\n\n\n\n<li>Promotes code reusability<\/li>\n\n\n\n<li>Simplifies large projects<\/li>\n\n\n\n<li>Improves maintenance<\/li>\n\n\n\n<li>Supports real-world modeling<\/li>\n\n\n\n<li>Makes applications scalable<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Real World Examples of OOP<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Classes and objects are used in:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Student Management Systems<\/li>\n\n\n\n<li>E-commerce Websites<\/li>\n\n\n\n<li>Banking Applications<\/li>\n\n\n\n<li>Hospital Management Systems<\/li>\n\n\n\n<li>Content Management Systems<\/li>\n\n\n\n<li>Online Booking Platforms<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Best Practices<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use meaningful class names<\/li>\n\n\n\n<li>Keep methods simple and focused<\/li>\n\n\n\n<li>Reuse code whenever possible<\/li>\n\n\n\n<li>Use constructors for initialization<\/li>\n\n\n\n<li>Follow object-oriented principles<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Classes and Objects are essential concepts in PHP Object-Oriented Programming. Classes define the structure, while objects bring that structure to life. Understanding OOP helps developers build professional, maintainable, and scalable web applications.<\/p>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/php\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Advanced PHP > Object-Oriented PHP > Classes and Objects<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1779275136556\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":37,"template":"","class_list":["post-123","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Classes and Objects - Learn PHP with GiGz.PK<\/title>\n<meta name=\"description\" content=\"Learn PHP classes and objects with practical examples and understand object oriented programming concepts easily.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/php\/?lesson=classes-and-objects\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Classes and Objects - Learn PHP with GiGz.PK\" \/>\n<meta property=\"og:description\" content=\"Learn PHP classes and objects with practical examples and understand object oriented programming concepts easily.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/php\/?lesson=classes-and-objects\" \/>\n<meta property=\"og:site_name\" content=\"Learn PHP with GiGz.PK\" \/>\n<meta property=\"article:modified_time\" content=\"2026-05-21T14:40:21+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=classes-and-objects\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=classes-and-objects\",\"name\":\"Classes and Objects - Learn PHP with GiGz.PK\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\"},\"datePublished\":\"2026-05-20T11:05:34+00:00\",\"dateModified\":\"2026-05-21T14:40:21+00:00\",\"description\":\"Learn PHP classes and objects with practical examples and understand object oriented programming concepts easily.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=classes-and-objects#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=classes-and-objects\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?lesson=classes-and-objects#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/php\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Advanced PHP > Object-Oriented PHP > Classes and Objects\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/php\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/php\\\/\",\"name\":\"Learn PHP with GiGz.PK\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/php\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Classes and Objects - Learn PHP with GiGz.PK","description":"Learn PHP classes and objects with practical examples and understand object oriented programming concepts easily.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/php\/?lesson=classes-and-objects","og_locale":"en_US","og_type":"article","og_title":"Classes and Objects - Learn PHP with GiGz.PK","og_description":"Learn PHP classes and objects with practical examples and understand object oriented programming concepts easily.","og_url":"https:\/\/gigz.pk\/php\/?lesson=classes-and-objects","og_site_name":"Learn PHP with GiGz.PK","article_modified_time":"2026-05-21T14:40:21+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/php\/?lesson=classes-and-objects","url":"https:\/\/gigz.pk\/php\/?lesson=classes-and-objects","name":"Classes and Objects - Learn PHP with GiGz.PK","isPartOf":{"@id":"https:\/\/gigz.pk\/php\/#website"},"datePublished":"2026-05-20T11:05:34+00:00","dateModified":"2026-05-21T14:40:21+00:00","description":"Learn PHP classes and objects with practical examples and understand object oriented programming concepts easily.","breadcrumb":{"@id":"https:\/\/gigz.pk\/php\/?lesson=classes-and-objects#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/php\/?lesson=classes-and-objects"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/php\/?lesson=classes-and-objects#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/php"},{"@type":"ListItem","position":2,"name":"Advanced PHP > Object-Oriented PHP > Classes and Objects"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/php\/#website","url":"https:\/\/gigz.pk\/php\/","name":"Learn PHP with GiGz.PK","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/php\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/lesson\/123","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/php\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=123"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}