Professional Documents
Culture Documents
Creating Class
Let's start with a simple example.
Save the following in a fi
le called class.php
<?php
class Demo
{
}
?>
# construct an object
$name = new ClassName(parameters);
# access an object's field (if the field is
public)
$name->fieldName
# call an object's method
$name->methodName(parameters);
PHP
Adding Method
The Demo class isn't particularly
useful if it isn't able to do anything,
so let's look at how you can create a
method.
<?php
class Demo
{
function SayHello($name)
{
echo Hello $name !;
}
}
?>
Adding Properties
Adding a property to your class is as
easy as adding a method.
<?php
class Demo
{
public $name;
function SayHello()
{
echo Hello $this->$name !;
}
}
?>
Object Instantiation
You can instantiate an object of type
Demo like this:
<?php
require_once('class.php');
$objDemo = new Demo();
$objDemo->name = mbstechinfo;
$objDemo->SayHallo();
?>
Example on Class
Calling Member
Functions
Private
members are only accessible to the class itself
Protected
members are available to the class itself, and to
Public classes
is the default
visibility
levelitfor any member variables
that inherit
from
or functions that do not explicitly set one, but it is good
practice to always explicitly state the visibility of all the
members of the class.
Class Constants
It is possible to define constant values
on a per-class basis remaining the same
and unchangeable.
Constants differ from normal variables in
that you don't use the $ symbol to
declare or use them
The value must be a constant
expression, not (for example) a variable,
a property, a result of a mathematical
operation, or a function call
<?php
classMyClass
{
constconstant='constantvalue';
functionshowConstant()
{
echoself::constant."\n";
}
}
echoMyClass::constant."\n";
?>
Static Keyword
Declaring class properties or
methods as static makes them
accessible without needing an
instantiation of the class.
A property declared as static can not
be accessed with an instantiated
class object
Contructor
Constructor is the method that will
be implemented when object has
been initiated
Commonly, constructor is used to
initialize the object
Use function __construct to create
<?php
constructor
in PHP
class
Demo
{
function __construct
{
}
}
Constructor Functions:
Constructor Functions are special
type of functions which are called
automatically whenever an object is
created.
PHP provides a special function called
__construct() to define a
constructor. You can pass as many as
arguments you like into the
constructor function.
Destructor
Destructor, is method that will be run
when object is ended
<?php
class Demo
{
function __destruct
{
}
}
?>
Inheritance
There are many benefits of
inheritance with PHP, the most
common is simplifying and reducing
instances of redundant code
Interfaces:
Interfaces are defined to provide a common
function names to the implementors.
Syntax:
Abstract Classes:
An abstract class is one that cannot be
instantiated, only inherited. You declare an
abstract class with the keyword abstract,
like this:
interface InterfaceName {
public function name(parameters);
public function name(parameters);
...
}
class ClassName implements InterfaceName { ...
PHP
PHP
Final
If the class itself is being defined final
then it cannot be extended.
<html>
<body>
<?php
$handle=fopen("welcome.txt","r") ;
if($handle)
{
echo File opened ok.;
}
?>
</body>
</html>
Closing a File
The fclose() function is used to close
an open file:
Check End-of-file
The feof() function checks if the
"end-of-file" (EOF) has been reached.
Cannot read from files opened in w,
a, and x mode!
<?PHP
$file_contents =
readfile("dictionary.txt");
print $file_contents;
?>