PHP & OOP: Instance of a class

This entry is part 4 of 11 in the series PHP & OOP

The keyword new is to create an instance of a class.

<?php
class SimpleClass
{
    public $var = 'a default value';

    public function displayVar() {
        echo $this->var;
    }
}

//create an instance of the class SimpleClass
$simple_object = new SimpleClass();
echo $simple_object->displayVar();

?>

As in the line

$simple_object = new SimpleClass();

You can also think of it as creating an object from the class SimpleClass.  With the instance $simple_object, we can access the class variable using

$simple_object->displayVar()

In PHP, we have used -> operator.  In other programming languages such as C++, we would have used the dot(.) operator.

PHP & OOP: A simple class declaration

This entry is part 3 of 11 in the series PHP & OOP

We first look at this simple class declaration.

<?php
class SimpleClass
{
    // property declaration
    public $var = 'a default value';

    // method declaration
    public function displayVar() {
        echo $this->var;
    }
}

$simple_object = new SimpleClass();
echo $simple_object->displayVar();

?>

In object-oriented terminology, a property is actually the variable of a class and a method is a function declared inside the class.

In the above example, we have used the method displayVar() to access the variable $var.  The variable $var is a property of the class SimpleClass.

Visibility

The visibility or scope of a property or method can be defined by prefixing the declaration with the keywords public, protected or private.

Class members declared public can be accessed everywhere.

Members declared protected can be accessed only within the class itself and by inherited and parent classes.

Members declared as private may only be accessed by the class that defines the member.

PHP & OOP: The OOP core ideas

This entry is part 2 of 11 in the series PHP & OOP

What made a language object-oriented?

There are a few core ideas related to Object Oriented programming language.

  • Dynamic Dispatch – when a method is invoked on an object,
  • Encapsulation – in which case the state is kept separate
  • Inheritance – inheritance lets the structure and methods in one class pass down the hierarchy

Additional concepts are classes and objects and we will start off our series with classes and objects.

PHP & OOP: The introduction

This entry is part 1 of 11 in the series PHP & OOP

Object-oriented programming is an approach to designing modular, reusable software systems.

Just like C, PHP was started out as a non object-oriented programming language.

Started in version 4, some basic elements of object oriented features were added.

And in version 5, especially from v 5.3, it started to support most features of object oriented methodology.

In some of our earlier examples, we have used object oriented style of programming.

You can refer to this example where I have used:

$mysqli = new mysqli('localhost', 'root', '', 'allartist')
$mysqli->query($query)
$result->fetch_assoc()

We could write those code in procedural style but learning about OOP will open up a whole world where we could reuse OOP code written by others.

HTML: A standard HTML5 page

This entry is part 1 of 34 in the series HTML

HTML stands for Hyper Text Markup Language.  It’s purpose is to use tags or markups to formulate a web page.

I have been using WebStorm from jetBrains and each time I create a new HTML page, it gives the following template.

You can use it likewise to create a HTML5 page.

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

</body>
</html>
Try The HTML

The doctype declaration will tell the browser that this is for HTML5.