0

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

    }

}

?>

Edited by crazyvonzipper: Added echo for testing

2
Contributors
8
Replies
22
Views
2 Years
Discussion Span
Last Post by pritaeas
0

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

    }

}

?>
0

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); */
        }

    }

}

?>
0

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

$myNsClass = '\myNamespace\someClass';

Edited by pritaeas

0

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.';
        }

    }

}
0

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

Edited by pritaeas

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.