0

Hi friends i am new to php , and i am trying to learn classes i am trying the following code with which i get all functions *name* described in the class can anybody tell me how can i print the output of functions in the class

<?php
    class dog {
                    public function bark() {
                    print "Woof!\n";
                    }

                    public function legs() {
                    print "four!\n";
                    }
             }

    class poodle extends dog {
      public function yip() {
            print "Yipppppppp!\n";
        }
    }

    $poppy = new poodle;

    //$poppy->bark();
    $class_methods = get_class_methods(new poodle());
    //echo    $class_methods;
        foreach($class_methods as $class_methods1)
        {
        echo $class_methods1.'<br/>';
        }

?> 

3 Answers 3

1

This should work:

$poodle = new poodle();
$class_methods = get_class_methods($poodle);
//echo    $class_methods;
foreach($class_methods as $class_method)
{
    echo $class_method.'\'s Output: '.$poodle->$class_method()."<br />"; 
}

In general:

If you have a value like $test = "abc" you can evaluate it (in php) to either a variablename, or a function etc.:

$test = "abc";
$test() // equal to abc() - if function abc exists.

echo $$test // equal to echo $abc - if $abc is defined.

$anotherTest = new $test(); // equal to new abc() - if class exists.
Sign up to request clarification or add additional context in comments.

2 Comments

THANKS THATS A PREETY NICE ANSWER WITH THIS I UNDERSTAND BOTH FUNCTION AND ITS OUTPUT
@RohitGoel Added a general example.
0
$poppy = new poodle;

$class_methods = get_class_methods($poppy);

foreach($class_methods as $class_method_name)
{
    echo $class_methods_name.' => '
        // Either:
        .$poppy->$class_method_name()
        // OR (prefered method, but more typing)
        .call_user_func(array($poppy, $class_method_name))

        .'<br/>';

}

1 Comment

thanks for replying but as a beginner this is little hard for me
0

A variable can be used in place of the method name to do what you want. Note: you don't need to echo the result of the method calls because your methods all print something but they don't actually return anything.

Demo

$poppy = new poodle;

$class_methods = get_class_methods($poppy);

foreach($class_methods as $class_methods1)
{
   $poppy->$class_methods1() . '<br/>';
}

Outputs

Yipppppppp!
Woof!
four!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.