What are Classes and Methods in OOP PHP

This tutorial is a beginners look at OOP(object oriented programming) in PHP and covers what a class, method and variables are as well as how to instantiate a class and add variables to it.

a class in OOP Php

A class in php is an object in object oriented programming or OOP, the class defines the object sort of a similar thing to a function in procedural php but the class is more of a higher level definition as known as an object (part of the object oriented programming) which encompasses more than a traditional function.

A class name is written in camel case format, defined like so…

<?php

 class MyExample {

}

?>

Listing all the classes

All classes defined in php are stored and can be listed with get_declared_classes();

$classes = get_declared_classes();

foreach ($classes as $class) {

    echo $class . "<br />";

}

So the above will output all native PHP classes plus any that are already declared or written in your program.

Testing for a class

You can also check if a class has been declared and if so do some code.

if( class_exists('MyExample') ) {

    echo 'Well lets write some code';

}

So what things can be inside a class?, well one of these things can be methods, another variables.

 

a method in OOP PHP

methods in PHP OOP are exactly like functions in procedural php, you can add in multiple methods to a class. They take the same format and are written as  normal functions.

class MyExample {

    function method_inside_example() {

        echo 'Method from inside example';

    }

}

 

So above is a class with a method inside called method_inside_example.

Listing all the methods

All methods declared also can be listed similar to classes with get_class_methods(‘AndTheNameOfTheClassGoesHere’)

$methods = get_class_methods('MyExample');

foreach ($methods as $method) {

    echo $method . '<br >';

}

So in the above all the methods of the class will be echoed out to the screen.

Testing for a method

You can also check if a method in a class exists similar to how a class exist and if so do some code.

if( method_exists('MyExample', 'method_inside_example' ) ) {

    echo 'Well lets write some more code';

}

Instantiate a class

How you actually get a class to run is not via the original declaration rather you create an instance of it which is known as instantiating a class. How you instantiate a class is by declaring the original class name with a prefix of new.

new MyExample();

So here we are creating a new copy of the original class MyExample() which then we need to assign to a variable

$classcopy = new MyExample();

Here instead we are both instantiating the class and assigning it to a variable at the same time.

To see what class an assigned variable belongs to, you can use get_class() – like so…

echo get_class($classcopy);

You can also test to see if that variable is a certain class by  stating the class name…

if( is_a($classcopy, 'MyExample' ) ) {

echo 'Yes I am a copy of MyExample';

}

else {

echo 'No i belong to someone else';

}

Calling the methods in a class

Now we have our instantiated class we can call our methods(functions) in our class, this is done by using our class variable with a dash+arrow head to the method…

$classcopy->method_inside_example();

So when declaring the above our method will run and we will get the result…

Method from inside example

It’s just like running a regular function but using the class instance and format with dash+arrow.

Variables in a class

Variables can be defined in orginal class declaration and need to be prefixed with var

class MyExample {
    
    var $blue_pill = '2 in the morning';
    var $red_pill = '1 in the night';
    
    function method_inside_example() {

        echo 'Method from inside example';

    }

}

So then you can access them via the instantiated class…

echo $classcopy->red_pill;

echo $classcopy->blue_pill;

Note that you don’t use the $ sign in the variable.

Now you can change the variable for the new instantiated class.

$classcopy->red_pill = '15 in the morning';

$classcopy->blue_pill = '12 at night time';

Now run the $classcopy

echo $classcopy->red_pill;

echo $classcopy->blue_pill;

And you will see the different result. This is some of the power in classes and variables with the ease to make copies of the classes and create different values for the variables.

Listing all the variables

Get a list of all the variables declared in a class…

$vars = get_class_vars('MyExample');

foreach ($vars as $var => $value) {

echo "{var}: {$value}<br >";

}

So here the original variables of the class are outputted as well as their value with the key/value pair $var=>$value.

Testing for a variable/property

You can also test for a variable (also known as a property) if it exists in a class

if( property_exists('MyExample', 'blue_pill') ) {

echo 'They do exist';

}

else {

echo 'Sorry only red ones here';

}

Thats it for now, I hope to do another follow up tutorial with some more OOP soon.

 

Leave a Comment





%d bloggers like this: