Class function without new

Is it possible to call a function from a class without having to create the object?

For example:-
  class testMe
  {
      public function __construct() {
        }
      public function test66() {
        echo "It works";
        }
  }
call_user_func('testMe','::test66'); 

Open in new window


Which should display 'It works'.
tonelm54Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris StanyonWebDevCommented:
Only if the method is static


<?php
class Foo {
    public static function aStaticMethod() {
        // ...
    }
}

Foo::aStaticMethod();
?>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Chris StanyonWebDevCommented:
Hmmm. Just tested it on non-static methods and that seems to work as well!!

class testMe {
   public function __construct() { }

   public function test66() {
      echo "It works";
   }
}

testMe::test66(); // will echo It works

Open in new window

0
Ray PaseurCommented:
Yes.  It sometimes is used in the Singleton design pattern.  It does not work on any method that uses $this.  I think the definition of static methods does not require the use of the word, "static."

Example of a data base connectivity singleton:
<?php // RAY_database_singleton.php
error_reporting(E_ALL);


// SINGLETON DATA BASE CONNECTION CLASS
class Database
{
    // CLASS PROPERTIES ARE ALL PRIVATE
    private static $connection;
    private static $instance;

    // CONNECTION VALUES
    const DB_HOST = 'localhost';
    const DB_USER = '?';
    const DB_PASS = '?';
    const DB_NAME = '?';

    // NULLIFY THE CLONE
    final private function __clone() {}

    // OUR ONLY PUBLIC METHOD RETURNS THE CONNECTION
    public static function getConnection()
    {
        if (!self::$instance) self::$instance = new self();
        return self::$connection;
    }

    // CONSTRUCTOR RETURNS THE CONNECTION
    private function __construct()
    {
        self::$connection
        = new mysqli
        ( self::DB_HOST
        , self::DB_USER
        , self::DB_PASS
        , self::DB_NAME
        )
        ;
        if (self::$connection->connect_error)
        {
            trigger_error(self::$connection->connect_error, E_USER_ERROR);
        }
    }
}

$mysql1 = database::getConnection();
$mysql2 = database::getConnection();


// PROVE THAT THESE ARE THE SAME OBJECT http://php.net/manual/en/language.oop5.object-comparison.php
if ($mysql1 === $mysql2) echo 'EQUAL';

// SHOW THE OBJECT
var_dump($mysql1);

Open in new window

0
Chris StanyonWebDevCommented:
Just read up on this as I was surprised I could call a non-static method on a class.

Calling a non-static method in a static way will generate a STRICT notice, which, even with error_reporting(E_ALL) does not display the errors as of PHP <5.4. As of 5.4, E_ALL includes E_STRICT (I always assumed ALL meant ALL!)

So, bottom line - don't call a non-static method in a static way. It will generate a warning even if you don't see it!

Note to self: need to use error_reporting(E_ALL | E_STRICT) from here on in ;)
0
Ray PaseurCommented:
@tonelm54: It's usually a good idea to read the online manual.  These references will be helpful.

http://php.net/manual/en/language.oop5.basic.php
http://php.net/manual/en/language.oop5.static.php
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.