Object-Oriented Programming Concepts in PHP
- Class
- Objects
- Inheritance
- Interface
- Abstraction
- Polymorphism
Class:- Class is a programmer-defined data type or Blue print of object.
- Class definitions begin with the keyword class
- A class may contain its own constants, variables (called "properties"), and functions (called "methods").
- Class is a collection of objects. Object has properties and behavior.
- A valid class name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.
Example:
<?PHP // Class declaration class firstclass{ // property declaration public $var = 'Hello World'; // method declaration public function displayVar() { echo $this->var; } } $objfirst = new firstclass; $objfirst->displayVar(); ?> OUTPUT: Hello World Creating Objects
- When class is created, we can create any number of objects to that class.
- It simply known as class properties and methods operate on data are bundled as a unit called as object.
- $this is available when a method is called from within an object context. Pointing the current object.
- create an instance of a class, the new keyword must be used. An object will always be created unless the object has a constructor defined that throws an exception on error.
No comments:
Post a Comment