Hi All.

For some odd reason it feels like this should be an extremely easy principle but yet I cannot seem to do this properly.

I know that there might be other ways to accomplish this but for the sake of my current project, i require this.

What i need is to call a method within a class that belongs to the global namespace from within a class that belongs to a defined namespace.

<?php

$myNsClass = 'myNamespace\someClass';
echo $myNsClass::printHello("John");

class myClass {

    function sayHello($name) {

        return 'Hello ' . $name;
    }

}

namespace myNamespace {

    class someClass {

        function printHello($name) {
            // call sayHello in myClass
            $classsss = new myClass();

            return $classsss->sayHello($name);
        }

    }

}

?>

Can't you just prefix the class with a \ ?

Could you please be a bit more specific ?

Goodness, I dont know if I am on the slow side today, but the whole code block seems to be fatally wrong?

<?php

$helloText = \myNamespace\someClass::printHello("John");
echo $helloText;

$myClass = new myClass();
echo $myClass->sayHello("John");

class myClass {

    function __construct() {
        echo __CLASS__;
    }

    function sayHello($name) {
        return 'Hello ' . $name;
    }

}

namespace myNamespace {

    class someClass {

        function __construct() {
            echo __CLASS__;
        }

        static function printHello($name) {
            $myClass = new \myClass();
            return $myClass->sayHello($name);
        }

    }

}

?>

I dont know how but my "actual" code works after adding the

\

to the class declaration.

What I am curious to know now though is why the following wont work (for infomative purposes?)

<?php

$myClass = new myClass();
echo $myClass->sayHello("John");

class myClass {

    function __construct() {
        echo __CLASS__ . ': ';
    }

    function sayHello($name) {
        return 'Hello ' . $name . '<br><br>';
    }

}

$myNsClass = 'myNamespace\someClass';
$helloText = $myNsClass::printHello("John");
echo $helloText;

namespace myNamespace {

    class someClass {

        function __construct() {
            echo __CLASS__ . ': ';
        }

        function printHello($name) {
            return 'Hello ' . $name . '<br><br>';


            /* $myClass = new \myClass();
              return $myClass->sayHello($name); */
        }

    }

}

?>

You want to dynamically pass the namespace? Looks like you forgot the \ again.

$myNsClass = '\myNamespace\someClass';

Well my real problem is that accessing classes in namespaces are very very tricky and even after I have read most of the php documentation, the issue i have seems like something that should work in theory but just simply does not work on my server php [PHP Version 5.3.13] & [PHP Version 5.4.36]

Here is an example:

$myNsClass = new myNamespace\myClass();
$myNsClass->isAlive();

namespace myNamespace {

    class myClass {

        function isAlive() {
            echo __CLASS__ . ': Is Alive.';
        }

    }

}

Define: "Does not work". Error messages, or nothing happens?

When I run your code I get:

Namespace declaration statement has to be the very first statement in the script in test.php on line 5

Your construct is listed as >= 5.3.0

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.