PHP OOP (Class,Method and Object)
PHP Class,PHP OOP |
PHP Class:
in php oop class begin with the keyword class, followed by a class name, followed by a pair of curly braces which enclose the definitions of the properties and methods belonging to the class.
Examples:
<?php //Declaration Class class Test{ //Declaration Property public $name='ANJAN KUMAR'; //Declaration Method public function User(){ return $this->name; } } //Access in Class $obj=new Test(); //echo result echo $obj->User(); ?>Output: ANJAN KUMAR
PHP Property:
When you create a variable inside a class, it is called a ‘property’.
public $name='ANJAN KUMAR'; is Property of Test Class..
PHP Method:
When you create a function outside of a class/object, you can call it a function but when you create a function inside
a class, you can call it a method.
A class’s methods are used to manipulate its own data / properties.
public function User() is Method. Note:Don't Forget.. in a class, variables are called ‘properties’ and functions are called ‘methods’.
Example 2:
<?php //Declaration Class class Studends{ //Declearation Property public $name; public $age; //Declaration Method public function StudendName(){ return 'Your Name Is: '.$this->name.' '; } public function StudendAge(){ return 'You Are '.$this->age.' Years Old'; } } //Calling a Class $ObjName=new Studends(); //Setting Students Name and Age $ObjName->name="ANJAN KUMAR"; $ObjName->age="20"; //Echo Result echo $ObjName->StudendName(); echo $ObjName->StudendAge(); ?>Output:
Your Name is: ANJAN KUMAR
You Are: 20 Years Old
I hope You Understand class,property and method in php oop. please keep visiting for next tutorials. Thank you for visit...
No comments:
Post a Comment
Thank You for Your Comment
Note: Only a member of this blog may post a comment.