user1930857
user1930857

Reputation:

How to use PHP Namespace in code

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');

Upvotes: 1

Views: 1095

Answers (1)

Debflav
Debflav

Reputation: 1151

Introduction

For your information: "Function names use underscores between words, while class names use both the camelCase and PascalCase rules."

So, I will use the PascalCase for your classes (avoid underscore).

New tree for your app

  • index.php
  • MyLibrary // Folder - contain all your classes
    • Person // Package
      • ReadMethods.class.php
      • Person.class.php
      • Behavior.class.php
    • Draw // Package
      • Circle.class.php

Adding namespace

MyLibrary/Person/ReadMethods.class.php

namespace Person;

interface ReadMethods 
{
    public function read_age($age);
}

MyLibrary/Person/Person.class.php

namespace Person;

abstract class Person 
{ 
    /* You should change your var to public/protected/private */
    var $gender;
    var $animal;
    var $birds;
    /* ... */
}

MyLibrary/Person/Behavior

namespace Person;

use \Circle\Draw; // use != require

class Behaviour extends Person implements ReadMethods
{   
    function get_all()
    {
        return $this->people();
        return $this->animals();
        return $this->bird();
    }
    /* ... */
}

MyLibrary/Draw/Circle.class.php

namespace Draw;

class Circle
{
    public $rad;
    function __construct($rad)
    {
        $this->rad=$rad;
    }
    function get_name($name)
    {
        return $this->rad * $this->rad * $name;
    }
}

index.php

/** Your custom autoloader **/
spl_autoload_register( function( $sClass) {
    /* Check File Existence. Define a path for your library folder */
    if(file_exists(YOUR_LIBRARY."{$sClass}.class.php")){
        if( !class_exists($sClass) ){
            require_once YOUR_LIBRARY."{$sClass}.class.php";
        }
    }
});

$doerte= new Person\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');

Go further

About the keyword use: Import class conditionally with the keyword 'use'

I hope it's will help and that i've been explicit.

Upvotes: 3

Related Questions