OOP (Object Oriented Programming) is a programming concept (or technique), which treats data and functions as objects.
First, important to this concept is to understand the difference between a Class and an Object.
Classes
- A class is a "blueprint" for an object, is a code template used to generate objects. It contins the code for properties and methods.
Objects
An object is the copied and active form of a class. It's an instance of its class, copied into memory, and can use the properties and methods defined in the class.
For example, to understand, you can think of a class as a blueprint for constructing an house. Many houses can be built from the same blueprint, so, the object is the house created according to that blueprint. You can build multiple objects that have the properties defined in the class.
- An object is a concrete entity constructed using the blueprint provided by a class.
• In OOP (Object Oriented Programming) are encountered the terms: "Encapsulation", "Inheritance" and "Polymorphism".
Encapsulation
- Encapsulation is the ability of an object to protect the data (properties) of a class from outside influences. You can use a class (its properties) through an object instance, without having access to its code.
Inheritance
- Inheritance is the ability of a class to copy or inherit the properties and methods of a parent class.
Polymorphism
Polymorphism is a class ability to do more different things, or use one class or another to achieve the same thing, having similar functions.
<?php class ClassName { // class body } ?>A good rule to follow is to put each class into its own file and to name that file "class.[ ClassName ].php".
<?php class Class_Name { attribute $property1; attribute $property2; ... attribute function Method1() { // method code } attribute function Method2() { // method code } ... } ?>
<?php // The class SiteClas class SiteClas { public $site = ' https://coursesweb.net/'; // public property private $category = 'php-mysql/'; // private property // defining method, with an argument public function pages($pag) { // output the URL address consists of the value of the two properties and its argument echo '<br />'. $this->site. $this->category. $pag; } } ?>- This is the class named SiteClas. To can use it in any script, save it in a separate file, called "class.SiteClas.php".
When accessing properties, you need only one $. The syntax is $obj->property
The property variable is declared as public $property and accessed using $obj->property.
$object_Name = new Class_Name();- $object_Name is the name of the object by which can be used properties and methods of that class.
<?php include('class.SiteClas.php'); // Include the class $objSite = new SiteClas(); // create an object instance of the SiteClas echo $objSite->site; // output the value of "site" property // calls the "pages" method [with an argument, as it was defined] $objSite->pages('php-oop-classes-objects'); // change the value of the "site" property, and calls the pages() method() $objSite->site = 'marplo.net/'; $objSite->pages('oop-clase-obiecte.html'); ?>- As you can see, first you must include the file containing the class, with the include() function.
$object->element- "element" can be any public property or public method.
The value assigned to a property through the instance of an object can be of any type: string, number, boolean, array or another object.
<ul> <li>http://coursesweb.net/html/</li> <li>http://coursesweb.net/css/</li> </ul>
.some_class { display: list-item; }
var obj = { "courses": ["php", "javascript", "ajax"] }; var jsonstr = JSON.stringify(obj); alert(jsonstr); // {"courses":["php","javascript","ajax"]}
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>'; $dochtml = new DOMDocument(); $dochtml->loadHTML($strhtml); $elm = $dochtml->getElementById("dv1"); echo $elm->nodeValue; // CoursesWeb.net