PhP Classes and Objects Explained
- Last Updated On: May 7, 2013
- By: Scriptcrunch Editorial
Class: Class is a way to organize the data. For instance , you want to write a program to display the names of parts of a car. What you can do is you can echo out all the part names of a car. So , now you want to display the part names of 10 cars . Now echoing out sounds little bitter, isnt it? here is where object oriented concepts comes in to play. To solve the problem, we use the class concept. here int this tutorial i have created a class called coder, which contains the coders details. So we create a class template called coder. Then we create many instances of the coder class to display the details of various other coders.
Class systax:
<?php
class coder{
{
public $variable;
private $variable;
public fucntionS()
{
//your code here
}
} ?>
Object: Object is an instance of the class. class is like a template and we can create many copies of the class using the object.
Object Syntax:
<?php $object1 = new coder() //here we create an on bject of type coder class using the keyword new. It means we create a object variable to connect to the class coder ?>
Example:
<?php
class coder {
public $name;
public $language;
public $position;
function showDetails($a, $b ,$b)
{
$this->name =$a;
$this->language =$b; //$this keyword assigns the parameter value to the variables declared as public.
$this->position =$c;
}
function thisTest($a)
{
$this->name = $a; //$this keyword here assigns the parameter to the variable name associated with function thisTest.
}
}
?>
<html>
<title>OOP demo</title>
<body>
<h1>This ia a php object oriented demo</h1>
<?php
$person1 = new coder(); //$person1 object created with new keyword
$person1->showDetails("michael","php","junior programmer"); //using the person object , here we pass the parameters to the showDetails fucntion.
echo "<div style='background-color:blue'>".$person1->name."  ".$person1->language."</div><br/>";
$person1->thisTest("libin");
echo "<div style='background-color:blue'>".$person1->name."</div>";
?>
</body>
</html>
Scriptcrunch Editorial
ScriptCrunch is your go-to website for all things DevOps. We specialize in helping engineers master Kubernetes and other essential DevOps tools. Our main goal is to provide easy-to-follow tutorials and valuable resources, with a special focus on the Cloud Native Computing Foundation (CNCF). Additionally, we offer insights into Linux Foundation Training and its various initiatives.
Other Interesting Blogs
[50% OFF] Kubernetes Certification Coupons (CKA, CKAD, CKS, KCNA and KCSA)
Looking for CKA coupon and other Kubernetes certification coupon codes? You have come to the right place. Following are the list of
[80% OFF] Linux Foundation Coupon for December 2024
Looking for CKA Coupon and other Linux Foundation certifications coupons? We got you covered. With the latest Linux Foundation Coupon, you can
[65% OFF] Linux Foundation LFCA, LFCS & LFCT Exam Voucher Codes
Linux Foundation has announced up to a $284 discount on its Linux certification programs Linux Foundation Certified IT Associate (LFCA) and Linux