Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lara120618's avatar

How to call object property as method?

Is there a way to dynamically call an object property as a method.

For instance.

class ObjA
{
    public $appPath;
}


$objA = new ObjA;

$objA->appPath();
0 likes
11 replies
Nakov's avatar

No, because that's not a method. There is nothing dynamic in what you've shown, you can call it simply like:

$objA = new ObjA;

$objA->appPath;

or define a function

class ObjA
{
    public function appPath() {}
}

Then call it as you've shown.

Nakov's avatar

@thavarshan can you give some code to what you are referring, url to the source code?

lara120618's avatar
<?php

namespace Illuminate\Routing;


/**
 * @method \Illuminate\Routing\Route get(string $uri, \Closure|array|string|null $action = null)
 * @method \Illuminate\Routing\Route post(string $uri, \Closure|array|string|null $action = null)
 * @method \Illuminate\Routing\Route put(string $uri, \Closure|array|string|null $action = null)
 * @method \Illuminate\Routing\Route delete(string $uri, \Closure|array|string|null $action = null)
 * @method \Illuminate\Routing\Route patch(string $uri, \Closure|array|string|null $action = null)
 * @method \Illuminate\Routing\Route options(string $uri, \Closure|array|string|null $action = null)
 * @method \Illuminate\Routing\Route any(string $uri, \Closure|array|string|null $action = null)
 * @method \Illuminate\Routing\RouteRegistrar as(string $value)
 * @method \Illuminate\Routing\RouteRegistrar domain(string $value)
 * @method \Illuminate\Routing\RouteRegistrar middleware(array|string|null $middleware)
 * @method \Illuminate\Routing\RouteRegistrar name(string $value)
 * @method \Illuminate\Routing\RouteRegistrar namespace(string $value)
 * @method \Illuminate\Routing\RouteRegistrar prefix(string  $prefix)
 * @method \Illuminate\Routing\RouteRegistrar where(array  $where)
 */
class RouteRegistrar
Nakov's avatar
Nakov
Best Answer
Level 73

So that class is extended by the Illuminate\Routing\Router class which then uses the Macroable trait in which the method mixin is defined. So again that's a method not a property.

And in the RouteRegistrar class you can see the __call method which acts whenever a method that does not exists on the parent class propagates the call to the child classes which in this case is as I said above the Router class that has the mixin method. I hope it is a bit more clear now.

lara120618's avatar

Oh thank you so much. I've been searching for this information all over. Thank you!!!

lara120618's avatar

Do you have any tips on how I can make my own Macroable trait?

Nakov's avatar

You can probably follow the Laravel source code to do that, I really don't know your needs in order to help you more.

lara120618's avatar

Yeah I've been trying to but for my understanding the Laravel code base is really complicated.

I'm basically trying to acheive the fluent routing thing that's being implemented on the Laravel router.

Another instance is in the RouteServiceProvider,

Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));

specifically this part,

->namespace($this->namespace)

I don't understand how the namespace is being set through the UrlGenerator and the controller actions are being formatted to incorporate the full namespace.

Nakov's avatar

I believe the Builder pattern is used there, so you can take a look at that design pattern. Mainly you will need to check what the previous method returns, in your example above, you need to check what does Route::middleware('web') returns, that's where the namespace method exists so the call can be chained.

Please or to participate in this conversation.