OOPS Classes and Objects EP-1

OOPS Classes and Objects EP-1

Recap

The Previous blog said that a Class is the blueprint of a class and an object is an instance of class from that blueprint you can build houses.

Object data type

Php has a generic class implementation called std(Standard) class that you could use to create generic objects or you could cast the variables that are off and other data types into objects.

  • Through this, you can create objects using generic std Class

  • We can also create your custom classes

Now, Lets create a class called Transaction and There are some rules for creating a class

  • class keyword should be used to name a class

  • The class name should start with a letter or _ underscore

  • use a curly brace to enclose the class definition

  • Our file name and class name could be same for naming conventions and your wish you can name it as per your view.

  • We can have more than one class in a single file But it's not recommended, recommended way is to have a single class per file.

<?php
class Transaction
{
//write your class definition here
}
?>

Now you can create an object for this Transaction class and you can create it within the same class.But it's not recommended way the usage of a class object is to acquire the properties and methods of a class from the outside of a class is what makes oops a good concept.

so, now im going to create a class in an index.php

//file index.php
<?php
$transaction = new Transaction();//parentheesis not always required
?>

object creation

  • In object, creation parenthesis isn't always required

  • It's depending upon your constructor

  • We have to Load the class file in our index.php file to get access(permission)from the class to use its property and methods using our transaction object.

  • To load class files advanced concepts are there namespace and autoloading we will see later.

//file index.php
<?php
$transaction = new Transaction();//parentheesis not always required
var_dump($transaction);
?>

The var_dump() function dumps information about one or more variables. The information holds the type and value of the variable(s). Using it we can see our class object.

Our object

  • It's an empty object datatype as object.

  • It's an instance of a transaction class.

Did you find this article valuable?

Support NaveenJoshuva by becoming a sponsor. Any amount is appreciated!