dkode
dkode

Reputation: 206

How to access class members using namespace?

Learning with PHP Namespaces but stuck into basics. below is my class

<?php namespace NS;

class H {

    public $v = 'now';
    public static $v_ = 'static';

    public function pm() {
       printf("<br/>public %s; variable v:%s", __METHOD__, $this->v);
    }

    public function getList()
    {
        printf("<br/> %s; Variable md5: %s", __METHOD__, md5($this->v));
    }

    public static function gl_()
    {
       printf("<br/> %s ; var: %s ; static: %s", __METHOD__, $this->v, self::$v_);
       self::getList();
    }


}

created a class object ( works fine )

$o = new H;
var_dump($o); // object(NS\H)[1]  public 'v' => string 'now' (length=3)

Now when I tried to access class methods ( public for now ) getting help from namespace documentation

with Unqualified name

$o_ = new getList(); 

gives

Fatal error: Class 'NS\getList' not found

with Qualified name

$o_ = new H\getList();

gives

Fatal error: Class 'NS\H\getList' not found

with Fully qualified name

$o_ = new NS\H\getList(); 

gives

Fatal error: Class 'NS\NS\H\getList' not found

Please help me to fix this namespace issue.

Thank you very much.

Upvotes: 0

Views: 839

Answers (1)

midnight-coding
midnight-coding

Reputation: 3227

Classes And Namespaces

First off you are trying to instantiate your class 'method'. This cannot be done. Instead, you instantiate your 'class' first then you call your 'method'.
http://php.net/manual/en/language.oop5.basic.php

Next, if you are not using an autoloader you must 'include' or 'require' the file containing the class you wish to use.
http://php.net/manual/en/function.include.php
http://php.net/manual/en/function.require.php

Instantiation & Method Calling

File Structure

+ Test.php   (file)
+ NS         (directory)
   + H.php   (file)

File 'H.php'

<?php

namespace NS;

class H
{
    public $v = 'now';
    public static $v_ = 'static';

    public function pm()
    {
        printf("<br/>public %s; variable v:%s", __METHOD__, $this->v);
    }

    public function getList()
    {
        printf("<br/> %s; Variable md5: %s", __METHOD__, md5($this->v));
    }

    public static function gl_()
    {
       printf("<br/> %s ; var: %s ; static: %s", __METHOD__, $this->v, self::$v_);
       self::getList();
    }
 }

File 'Test.php'

<?php

// Include the file.
include 'NS\H.php'; // Delete if using an autoloader.

// Instantiate the object.
$o = new NS\H(); // Note: The 'NS\' (being your namespace and separator) after the 'new' keyword.
                 //       The '()' are required after your class name.

// Call the objects method.
$o->getList();

?>

Namespaces

Secondly, regarding the namespace issue, there are multiple ways one can do this.
http://php.net/manual/en/language.namespaces.php

File 'Test.php' - Option 1 (As shown above)

<?php

// Include the file.
include 'NS\H.php'; // Delete if using an autoloader.

// Instantiate the object.
$o = new NS\H();

// Call the objects method.
$o->getList();

?>

File 'Test.php' - Option 2

<?php

// Include the file.
include 'NS\H.php'; // Delete if using an autoloader.

// Alias the class.
use NS\H;

// Instantiate the object.
$o = new H();

// Call the objects method.
$o->getList();

?>

File 'Test.php' - Option 3

<?php

// Include the file.
include 'NS\H.php'; // Delete if using an autoloader.

// Alias the class.
use NS\H as H; // Exactly the same outcome as option 2.

// Instantiate the object.
$o = new H();

// Call the objects method.
$o->getList();

?>

File 'Test.php' - Option 4

<?php

// Include the file.
include 'NS\H.php'; // Delete if using an autoloader.

// Alias the class.
use NS/H as Abc;

// Instantiate the object.
$o = new Abc();

// Call the objects method.
$o->getList();

?>

Autoloading (Extra Reading)

Lastly, all of the above is really wrapped up nicely when one uses an autoloader.
http://php.net/manual/en/language.oop5.autoload.php

Using an autoloader eliminates the need to write 'include' and 'require' throughout your code. Whilst there is nothing wrong with this (this is the way autoloaders actually work) it will improve development time and reduce errors.

I hope this helps.

Upvotes: 1

Related Questions