I am quiet new to object oriented php. And learning step wise new things in it. Now I want to work with namespace in php. I have 2 files in 1 directory. And I want to use get_name() function from class.lib in index.php file using namespace but dont know how to use it. When I simply include file class.php into index.php its working fine but I want to use namespace instead.
index.php
<?php
interface read_methods
{
public function read_age($age);
}
abstract class person
{
var $gender;
var $animal;
var $birds;
abstract function group($group);
function people($value)
{
$this->gender=$value;
}
final public function animals($value)
{
$this->animal=$value;
}
function bird($value)
{
$this->birds=$value;
}
}
class behaviour extends person implements read_methods
{
function get_all()
{
return $this->people();
return $this->animals();
return $this->bird();
}
function non_human($nonhuman)
{
$this->non_human=$nonhuman;
}
function read_age($age)
{
try {
if($age > 20) {
throw new Exception('Age exceeds!');
}
else
{
$this->age=$age;
}
}
catch(Exception $e)
{
echo 'There has been an error for the age value : '.$e->getMessage().' <br>' ;
}
}
function group($group)
{
return $this->group=$group;
}
}
$doerte= new behaviour();
$doerte ->people(array('male','female'));
$doerte ->animals(array('fish','whale'));
$doerte ->bird(array('parrot','crow'));
$doerte->non_human('alien');
$doerte->read_age('19');
$doerte->group('living_things');
print_r($doerte);
?>
class_lib.php
<?php
class Circle
{
public $rad;
function __construct($rad)
{
$this->rad=$rad;
}
function get_name($name)
{
return $this->rad * $this->rad * $name;
}
}
$Cir = new \Circle(5);
echo $Cir->get_name('30');
var $gender;is "deprecated". If it's the case you can't usenamespacebecause you need PHP >= 5.3.0