Php-mysql Course

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.

Creating a Class

To create a class use the class keyword and a name. Class names can be any combination of numbers and letters, although they must not begin with a number.
The code associated with a class must be enclosed within braces.
<?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".

Setting Properties and Methods

In the class body are defined its properties and methods.
        - Properties are variables defined inside the class.
        - Methods are functions created within the class, with the word "function".
The name of the variable (property) and the "function" word must be preceded by a specific attribute that determines the level of access that is allowed for the property or the method to be accessed outside its class. This can be: public, protected, or private. If no attribute is added, it is considered "public".

  - Full syntax to create a class is:
<?php
class Class_Name {
  attribute $property1;
  attribute $property2;
  ...

  attribute function Method1() {
    // method code
  }
  attribute function Method2() {
    // method code
  }
  ...
}
?>

Here is an example of a simple class that contains two properties, one with the public attribute (called $site), the second with private (called $category) and a method with the public attribute (named "pages").
<?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".

- $this variable is used to make the object can get information about itself. "$this->site" indicates / makes to be called the "site" property of the current object of this class.

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.

Using a class, creating objects

Once a class is created, to be used in your script, you must instantiate an object of that class. The instance is declared in the PHP script with the new operator, followed by the name of the class and two parentheses.
  - Syntax:
$object_Name = new Class_Name();
- $object_Name is the name of the object by which can be used properties and methods of that class.

  - Example:
Here's how to use the SiteClas class defined above (for explanation, see the comments in code).
<?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.
- Before you can use the properties and methods defined in the class, you must create an object instance of the class, becouse only with that object the public properties and methods can be accessed, using the following sintax.
$object->element
- "element" can be any public property or public method.

By calling the "pages()" method, in the example above ( $objSite->pages('php-oop-classes-objects'); ) will be executed the code of the "pages()" function defined in the class, which displays a URL address with its argument.
- Notice that the value of the public properties can be modified in the script. The "site" property can be accesed to use its value as well as for adding a new value to it (here the string 'www.mnarplo.net/').
The example above will output:
https://coursesweb.net/
https://coursesweb.net/php-mysql/php-oop-classes-objects
marplo.net/php-mysql/oop-clase-obiecte.html

If you try to access a private property in the script, outside the class body (here "category"), for example:   $objSite->category; , will generate an error like the folowing:
Fatal error: Cannot access private property SiteClas::$category in C:\server\www\page.php on line 10

The value assigned to a property through the instance of an object can be of any type: string, number, boolean, array or another object.

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
PHP OOP - Creating Classes and Objects

Last accessed pages

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (141749)
  2. Node.js Move and Copy file (28420)
  3. MouseEvent - Events for Mouse (2909)
  4. PHPMailer (2311)
  5. Uploading images to server with Ajax (6095)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (473)
  2. CSS cursor property - Custom Cursors (79)
  3. The Mastery of Love (70)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (62)
  5. CSS3 2D transforms (46)