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

diractorhm's avatar

static, self or Model?

Hallo there, I wonder if someone could shed some light on the following question:

As an example, Inside my User model I am creating a public static function that creates a new User based on some data passed in. What is the right way to refer to the class or what is the difference respectively?

class User extends Model
{
	public static function($input) {
	    // ...do some stuff
	
	    // what is now the right way to get an instance?
				
	    $user = static::create($input);
	    // or
	    $user = self::create($input);
	    // or 
	   $user = User::create($input);

	    return $user;
    }
}

Thank you!

0 likes
3 replies
LaryAI's avatar
Level 58

When you're working within a class method and want to refer to the class itself, you have a few options in PHP: static, self, and the class name (e.g., User). Here's what each of them means and when you might choose one over the others:

  1. static: Refers to the called class in the context of late static binding. This means if you have a class hierarchy and static is used in a parent class, it will refer to the class that was initially called at runtime. This is useful when you have class inheritance and want to allow child classes to override static methods or properties.

  2. self: Refers to the class in which the new keyword is actually written. It does not account for inheritance and will always refer to the class where it is used, even if a child class is calling the method.

  3. User: Refers explicitly to the User class, regardless of any inheritance or context in which it is called.

In the context of your User model, here's how you might decide which to use:

  • If you want to ensure that the create method of the User class is always called, even if the method is called on a subclass of User, you should use User::create($input).
  • If you want to allow for the possibility that a subclass of User could override the create method and you want to call that method instead, you should use static::create($input).
  • If you are certain that the create method will not be overridden by a subclass, or you do not have any subclasses, you could use self::create($input), but this is less flexible than static.

Given that in most cases when using Eloquent models in Laravel (which I assume from the context of Model), you might want to allow for the possibility of extending the User model, the most flexible and commonly used approach would be to use static. This way, if you ever extend the User model, the extended model will use its own create method if it has one.

Here's the corrected code using static:

class User extends Model
{
    public static function createFromInput($input) {
        // ...do some stuff

        // the right way to get an instance, allowing for late static binding
        $user = static::create($input);

        return $user;
    }
}

Remember to give your method a name (I've used createFromInput as an example) and ensure that the create method is compatible with how Eloquent expects data for model creation.

martinbean's avatar

@adagps If you’re declaring a static method like that then it makes sense to just be consistent, and also use static inside the method to return to the current class.

amitsolanki24_'s avatar

If you are using static means you can access methods and variables without declaring the class object.

Mainly static function call directly like this Class_name::method_name();

Please or to participate in this conversation.