Creating a Class:
To create a class use the class keyword and a name. Class names can be any combination of numbers and letters, although they must not begin with a number.
The code associated with a class must be enclosed within braces.
Class Defination:
<?php
class ClassName {
// class body
}
?>
For example:
<?php
class Books{
public $name;
public $author;
public function showBook(){
$name="Munna Madan";
echo $name;
}
}
?>
Setting Properties and Methods
In the class body are defined its properties and methods.
- Properties are variables defined inside the class.
- Methods are functions created within the class, with the word "function".
The name of the variable (property) and the "function" word must be preceded by a specific attribute that determines the level of access that is allowed for the property or the method to be accessed outside its class. This can be: public, protected, or private.
public - available in all script
protected - available to the class itself and to sub-classes that inherit from it
private - available only inside that class
If no attribute is added, it is considered "public".
Full syntax to create a class is:
<?php
class Class_Name {
attribute $property1;
attribute $property2;
...
attribute function Method1() {
// method code
}
attribute function Method2() {
// method code
}
...
}
?>
The code associated with a class must be enclosed within braces.
Class Defination:
<?php
class ClassName {
// class body
}
?>
For example:
<?php
class Books{
public $name;
public $author;
public function showBook(){
$name="Munna Madan";
echo $name;
}
}
?>
Setting Properties and Methods
In the class body are defined its properties and methods.
- Properties are variables defined inside the class.
- Methods are functions created within the class, with the word "function".
The name of the variable (property) and the "function" word must be preceded by a specific attribute that determines the level of access that is allowed for the property or the method to be accessed outside its class. This can be: public, protected, or private.
public - available in all script
protected - available to the class itself and to sub-classes that inherit from it
private - available only inside that class
If no attribute is added, it is considered "public".
Full syntax to create a class is:
<?php
class Class_Name {
attribute $property1;
attribute $property2;
...
attribute function Method1() {
// method code
}
attribute function Method2() {
// method code
}
...
}
?>
Creating Objects:
Once a class is created, to be used in your script, you must instantiate an object of that class. The instance is declared in the PHP script with the new operator, followed by the name of the class and two parentheses.
Syntax:
$object_Name = new Class_Name();
- $object_Name is the name of the object by which can be used properties and methods of that class.
Now full example of Class and object below:
<?php
class Books{
public $name;
public $author;
function showName(){
$name="Munna Madan";
$author="Laxmi Prasad Devkota";
echo "Book named ".$name." was written by".$author;
}
}
$obj=new Books(); //creating an object
$obj->showName();// calling function via object
?>
Syntax:
$object_Name = new Class_Name();
- $object_Name is the name of the object by which can be used properties and methods of that class.
Now full example of Class and object below:
<?php
class Books{
public $name;
public $author;
function showName(){
$name="Munna Madan";
$author="Laxmi Prasad Devkota";
echo "Book named ".$name." was written by".$author;
}
}
$obj=new Books(); //creating an object
$obj->showName();// calling function via object
?>
I hope it was not very hard !!!
Comments
Post a Comment