PHP OOP – Classes & Object

PHP OOP – Classes & Object

In this post we will give you information about PHP OOP – Classes & Object. Hear we will give you detail about PHP OOP – Classes & ObjectAnd how to use it also give you demo for it if it is necessary.

Classes & Object are basic concept of oops in PHP (Object Oriented Programming).

Class :

Basic concept of class was introduced from php4 which is the main piller of oop but complete features of class is introduced in php5 such as access modifier or interface.

There are almost same basic architecture between php4 and php5 except use of visibility, we will define visibility in php classes in depth

It is very easy to define class in PHP.

Classes are simply defined by using class keyword in PHP.

While defining your class, do not create your class with name stdClass in PHP. stdClass is reserved keyword which you can’t re-define if you do the same then PHP will generate fatal error with following message.

Fatal error : Cannot redeclare class stdClass

Never create your function of the class starting with __like __call. Because you are noticed that magic function in php is started with __ (double underscore)

Follow some naming convention while creating class (If the class name contains more than one word, we use upper camel case to create class name. )

Objects :


Classes are nothing without objects. Object is a instance of class. Objects of class can be created by using new keyword

Note — PHP OOP allows objects to point out themselves using $this. You can use $this while working within a method but if you are working outside of class then you would use the object name.

$object= new myClass();

You can also create  multiple object for a class.

The process of creating an object is also known as instantiation.

You can create an object by creating a variable which store the information and using the new keyword with the name of the class to instantiate from.

As you can see in above image we are creating three objects(Mercedes, Bmw, and Audi.) and all object are created from same class name ‘car'(We can create many object as we would like from same class) and thus they have the class’s methods and properties, they are not same. this is not only because they have different names but they may have different values assigned to their properties.As in above image : They are differ by color property in sequence green, blue and orange

  1. class myClass
  2. {
  3. //variables of the class
  4. public $variable1;
  5. public $variable2;
  6. //Function of class
  7. functionaddVariables()
  8. {
  9. $result=$this->variable1 +$this->variable2;
  10. return$result;
  11. }
  12. }
  13. $object=newmyClass();
  14. $object->variable1=2;
  15. $object->variable2=3;
  16. $sumvalue=$object->addVariables();
  17. echo"Output will be :".$sumvalue;
class myClass{//variables of the classpublic $variable1;public $variable2;//Function of classfunction addVariables(){$result= $this->variable1 + $this->variable2;return $result;}}$object= new myClass();$object->variable1=2;$object->variable2=3;$sumvalue=$object->addVariables();echo "Output will be :".$sumvalue;

Label :

PHP

OOP

Object Oriented Programming

Web Development

Hope this code and post will helped you for implement PHP OOP – Classes & Object. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  47  =  53

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US