avien
avien

Reputation: 177

automatically call all method inside the class in php

Is there a way to call all the methods from a class once the class is initialized? For example, lets say I have a class named todo and once I make an instance of a todo class, all the methods/functions inside it will be executed, without calling it in the constructor?

<?php 
    class todo 
    {
        function a()
        {
        }
        function b()
        {
        }
        function c()
        {
        }
        function d()
        {
        }
    }

    $todo = new todo();
?>

In here I created an instance of a class todo so that the methods a, b, c, d will be executed. Is this possible?

Upvotes: 3

Views: 6859

Answers (6)

Ramesh_D
Ramesh_D

Reputation: 689

I copied and pasted below class from php.net...

I thought it will be usefull because methods are not called using objects, instead using get_class_methods():

class myclass {
    function myclass()
    {
        return(truenter code heree);
    }

    function myfunc1()
    {
        return(true);
    }

    function myfunc2()
    {
        return(true);
    }
}

$class_methods = get_class_methods('myclass');
foreach ($class_methods as $method_name) {
    echo "$method_name\n";
}

Upvotes: 1

Sherlock
Sherlock

Reputation: 7597

This outputs 'abc'.

class Testing
{
    public function __construct()
    {
        $methods = get_class_methods($this);

        forEach($methods as $method)
        {
            if($method != '__construct')
            {
                echo $this->{$method}();
            }
        }
    }

    public function a()
    {
        return 'a';
    }

    public function b()
    {
        return 'b';
    }

    public function c()
    {
        return 'c';
    }
}

Upvotes: 6

aDaemon
aDaemon

Reputation: 131

I think you can use iterator. All methods will be called in foreach PHP Iterator

Upvotes: 2

Fluffeh
Fluffeh

Reputation: 33512

There isn't any way that I can think of, short of putting it into the constructor as you suggest in your question:

<?php 
    class todo 
    {
        public function __construct()
        {
            $this->a();
            $this->b();
            $this->c();
            $this->d();
        }
        function a()
        {
        }
        function b()
        {
        }
        function c()
        {
        }
        function d()
        {
        }
    }

    $todo = new todo();
?>

Upvotes: 1

Lucas Green
Lucas Green

Reputation: 3959

Like this? I use this pattern to register meta data about classes sometimes.

<?php
class todo {
    public static function init() {
        self::a();
        self::b();
        self::c();
        self::d();
    }
    function a()
    {
    }
    function b()
    {
    }
    function c()
    {
    }
    function d()
    {
    }
}

todo::init();

Upvotes: 1

alex
alex

Reputation: 490253

Use a __construct() method (as you mentioned), which is called on object instantiation. Anything else would be unfamiliar and unexpected (to have random methods instantly executed not by the constructor).

Your class code looks like you're using PHP4, if that's the case, name your constructor the same as the class name.

Upvotes: 1

Related Questions