Classes in PHP


Classes


Basic class definitions 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.

The class name can be any valid label, provided it is not a PHP reserved word. A valid class
name starts with a letter or underscore, followed by any number of letters, numbers, or
underscores. As a regular expression, it would be expressed thus: ^[a-zA- Z_\x7f-\xff][a-
zA-Z0- 9_\x7f-\xff]*$.

A class may contain its own constants, variables (called "properties"), and functions (called
"methods").


Example #1 Simple Class definition-Program:

<?php
class A
{
function foo()
{
if (isset($this)) {
echo &#39;$this is defined (&#39;;
echo get_class($this);
echo &quot;)\n&quot;;
} else {
echo &quot;\$this is not defined.\n&quot;;
}
}
}
class B
{
function bar()
{A::foo();
}
}
$a = new A();
$a-&gt;foo();
A::foo();
$b = new B();
$b-&gt;bar();
B::bar();
?>


Output:
$this is defined (A)
$this is not defined.
$this is defined (B)
$this is not defined.

Custom Search

%d bloggers like this: