skip to Main Content

I want to know if there is a way to namespace values, in a similar way that we namespace functions in PHP.

You can have:

namespace pizzaland;
function turn_oven_on(){}

And you can access that function with pizzalandhello()

I wonder if there is a way to do something similar for values.

This is not correct, I am just illustrating what I mean:

namespace pizzaland;
namespaced $ingredients = array('pepperoni', 'garlic');

Then access it with $pizzalandingredients.

Other parts in the same runtime can do:

namespace pastaland;
namespaced $ingredients = array('tomato', 'mozzarella');

Then access it with $pastalandingredients.

Of course that doesn’t work, but it serves as an example of what I mean.

I know there is the obvious way, which would be to use the Singleton pattern where the constructor sets the value of a public property for the singleton instance (the one and only one instance of the class).

I dislike this setup, and in that case I prefer to go the killer route and just do global $pseudonamespaced_pizza_land_ingredients.

I wonder, is there anything else I can do to achieve this setup using the latest version of PHP (now 8.1)?

Why?

To have the same effect you have with global but at the same time avoid collision.

Well, let’s say I am working with some procedural code and I need a value that can be accessed across multiple functions.

So I want to use that value within the realms of that bunch of functions.

I do not want to wrap all those functions into one Class and then use a property for that class, because in that case I just prefer the Singleton or the global.

Also, if not possible. Why not?

I cannot believe that this hasn’t been mentioned before as something to consider for integration into PHP. So, there must be a reason for this not being possible, if it isn’t. It would be a cool solution for all those codebases that are mostly procedural and use global way too often… ehem… WordPress…

2

Answers


  1. Chosen as BEST ANSWER

    Thanks to @shingo's comment under the question, this is how I now learned that this can be achieved.

    // Namespace
    namespace whatever;
    
    // Class with static property
    class StaticStuff {
        static string $variable = 'Hey';
    }
    
    // Access it
    echo StaticStuff::$variable; // Hey
    
    // Edit it
    StaticStuff::$variable = 'Miau!';
    
    // Access it again
    echo StaticStuff::$variable; // Miau!
    

    Specifically:

    • Define a class
    • With a static property
    • Inside a namespace

  2. I think this could be a good answer for this question as the goal is to have variable live inside of the namespace, and that OP has overcomplicated the question for no reason.

    Assuming that you have a variable named $number in the global scope of a PHP script. Inside a function, we have another variable with the same name $number, and we assign and change values of it within the function. But, the global variable remains unchanged. This is variable scope. In the same way, classes can be namespaced to give it scope.

    Consider having a following code:

    <?php
    namespace Math;
    
    function add($a, $b) {
        return $a + $b;
    }
    const PI = 3.14;
    
    class Geometry {
        static function getCircleArea($radius) {
            return PI * $radius ** 2;
        }
    }
    
    1. First, we declare the namespace. (So, all the classes, interfaces, constants and function below it will be items of that namespace)
    2. Then, we declared a normal function.
    3. Then, a class constant.
    4. Then, a class.

    Now let’s access to theseMath namespace’s items from another file(functions, constants, and classes).

    <?php
    // includes the Math.php file
    // It's like you had all the code of Math.php written here
    include_once 'Math.php';
    
    echo Mathadd(2,3); // 5
    echo MathPI; // 3.14
    echo MathGeometry::getCircleArea(10); // 314.15
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search