PHP : Difference b/w new self and new object() [duplicate]


This question already has an answer here:

<ul><li> <a href="/questions/2396415/what-does-new-self-mean-in-php" dir="ltr" rel="nofollow">What does new self(); mean in PHP?</a> <span class="question-originals-answer-count"> 5 answers </span> </li> </ul>

I was watching PHP video lectures . I have user class in my project directory now in tutorial inside a static function its instantiated like this

private static function instantiate($result){ $object = new self; //here goes loop }

and somewhere its used like this

$object= new user();

Will someone please guide me about the concept of first case where it says new self


"self" is a keyword that references the current class. It is only available inside the class' code.

You can use it to call methods on your own class, but due to binding you can also use it to call methods on a superclass.

Consider the following example:

class TestA { public static function makeNewInstance() { return new TestA(); } } class TestB extends TestA { }

Now, calling TestB::makeNewInstance(); will return an instance of TestA. (TestB inherits the method, but it's linked directly to TestA so will still return that)

Compare with this one:

class TestA { public static function makeNewInstance() { return new self(); } } class TestB extends TestA { }

Now, calling TestB::makeNewInstance() will return an instance of TestB. (Since self references the active class, and you're calling it on TestB, the contents of "self" is now TestB instead of TestA.

Hope that explains for you. Otherwise, maybe some more detail in your question would help attract more specific answers.


self points to the class in which it is written. Refer <a href="https://stackoverflow.com/questions/2396415/what-does-new-self-mean-in-php" rel="nofollow">What does new self(); mean in PHP?</a>

$object= new user(); // It makes the object of the class user - outside from the user class


  • How to make UI in blackberry including images in such a way that it works in different screen resolu
  • Escape input data in SQL queries when using ODBC + Access
  • SetHidden not working [duplicate]
  • RETROFIT how to parse this response
  • Are model beans singleton in spring?
  • Is there any point in using DI for class injections
  • Doc2vec : TaggedLineDocument()
  • JPS useBean with HashMap
  • Cannot instantiate the type Image java?
  • writing file in heroku filesystem and reading it with web app
  • Pointer vs Reference difference when passing Eigen objects as arguments
  • How to initialize context? [closed]
  • LNK1104: cannot open file 'kernel32.lib'
  • Graceful pod termination
  • iOS Cordova first plugin - plugin.xml to inject a feature
  • Adding a model to a Marionette CollectionView's collection doesn't trigger onItemAdd callb
  • Can my PDF ping my server when it is opened?
  • Pycharm: Marking a folder as 'sources root' is not recursive for subfolders
  • Mysterious problem with floating point in LISP - time axis generation
  • How to know when stdin is empty if it contains EOF?
  • If I include Java 8 in my Android app does that affect which devices it will work on?
  • Javascript + PHP Encryption with pidCrypt
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • Invalid access key error using credentials redeemed from an amazon open id token
  • Confusion with PayPal's monthly billing cycle
  • AT Commands to Send SMS not working in Windows 8.1
  • php design question - will a Helper help here?
  • PHP: When would you need the self:: keyword?
  • AngularJs get employee from factory
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • Acquiring multiple attributes from .xml file in c#
  • How do I configure my settings file to work with unit tests?
  • Getting Messege Twice Using IMvxMessenger
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Authorize attributes not working in MVC 4
  • Is it possible to post an object from jquery to bottle.py?
  • reshape alternating columns in less time and using less memory
  • Reading document lines to the user (python)
  • How can i traverse a binary tree from right to left in java?
  • How can I use threading to 'tick' a timer to be accessed by other threads?