calling class method (with constructors) without object instantiation in php

In PHP, is it possible to call a class’ member function (when that class requires a constructor to receive parameters) without instantiating it as an object?

In Previous version php doesn’t have support to do this, but we can do like this,

You can use an “factory”, sample:

<?php

class Foo
{
   private $__aaa = null;

   public function __construct($aaa)
   {
      $this->__aaa = $aaa;
   }

   public static function factory($aaa)
   {
      return new Foo($aaa);
   }

   public function doX()
   {
      return $this->__aaa * 2;
   }
}

Foo::factory(10)->doX();   // outputs 20

OR

you can try by using this code , here we are calling the function directly and storing in the variable.

$t = (new Test("Hello"))->foo("world");

 

 

Please follow and like us:
0

17 thoughts on “calling class method (with constructors) without object instantiation in php”

  1. Pretty section of content. I just stumbled upon your site
    and in accession capital to assert that I get actually enjoyed account your blog posts.
    Any way I’ll be subscribing to your augment and even I achievement you access consistently quickly.

  2. Hօwdy! Would yօu mind if I share your blog with my twitter group?
    There’s a lott of folks tht I think would rеаlly appreciɑte your
    content. Please llet me know. Thank you

  3. Hi! I know this is kinda off topic but I was wondering if you knew where
    I could get a captcha plugin for my comment form? I’m using the
    same blog platform as yours and I’m having trouble finding one?

    Thanks a lot!

  4. Thanks fоr the marveⅼous posting! I genuinely enjoyed reading it,
    you could be a great author. I will be sure to bookmark your
    blog and definitеly will come back later іn lifе.
    I want to encoᥙrage you to ultimately continue your great posts, have a nice
    afternoon!

  5. Good day! I ҝnow thiѕ iѕ kinda off topic but I
    was wondering which blog platform are you using for this site?
    I’m getting tirеd of WordPress because
    I’ve had problems with hackers and I’m looking at options for anothег platform.
    I would be fantastic if you couⅼd point me in thе diгection оf
    a ɡood platform.

  6. Veгy great post. I just ѕtumbled upon yoᥙr webⅼog and wisheɗ to say that
    I’ve truly enjoүed surfing around your weblog
    posts. After all I’ll be subscribing in your feed and I am hoping you
    wrіte again very soon!

  7. An interesting discussion is worth comment.

    I believe that you should publoish more about this issue, it might not bbe
    a taboo matter but typically people don’t speak about these issues.
    To the next! Cheers!!

  8. Whats up this is kinda of off topic but I was wondering
    if blogs use WYSIWYG editors or if you have to manually code with HTML.
    I’m starting a blog soon but have no coding experience so I wanted
    to get advice from someone with experience.

    Any help would be greatly appreciated!

Leave a Reply

Your email address will not be published. Required fields are marked *