Skip to main content

Calling a Class function without using $this->function_name() — PHP --



So I have this class:







class A{

public function do_a(){ return 'a_done';};



public function do_b(){ return 'b_done';};

}







So I require the php file and create an instance of the class:







require_once("A_class.php");

$System = new A();

require_once("user_calls.php"); //here I import the user file with the function calls.







user_calls.php contents:







echo 'this was the result of '.$System->do_a();

echo 'this was the result of '.$System->do_b();







So, that does work, but I don't want the user to have to use $System->do_a(); , but only do_a(); .





Any solutions?





EDIT: I also want to limit the functions the user could call in the user_calls.php file, to basic native php functions and those in class A.


Comments

  1. Methods of a class are either instance methods (they act on a particular instance of a class defined by $this) or they are class methods (They aren't tied to any one particular instance of a class, but provide services that fall within the remit of the class.

    An instance method is defined as follows:

    public function foo()
    {
    }


    whereas a class method is defined with the STATIC keyword.

    static public function bar()
    {
    }


    In the instance method you can use $this to get access to the state of the instance on which the method was called. This is not available in the class method because it's not tied to any one instance. It can access other members of the class (provided they're not tied to an instance) with the self keyword though.

    Instance methods are called as follows:

    $a = new ObjType ()
    $output = $a -> foo ();


    Class methods are called as follows:

    $output = ObjType::bar ();


    No matter which approach you use you either have to provide an instance (for instance methods) or a class (for class methods) to call the method. Calling just foo() or bar() will not work.

    ReplyDelete
  2. DISCLAIMER: While this code works, and does what you requested, that doesn't mean that I advocate coding like this. It's very hard to follow for other developers (and maybe even you in the future...), and it also makes use of eval(), which is almost always A Bad Thing(tm). That said, here you go:

    <?php
    class A {
    public function do_a() {
    return __METHOD__;
    }

    public function do_b() {
    return __METHOD__;
    }
    }

    $aRef = new ReflectionClass('A');
    $aPublicMethods = $aRef->getMethods(ReflectionMethod::IS_PUBLIC);

    foreach ($aPublicMethods as $method) {
    $php = <<<PHP
    function {$method->name}() {
    global \$System;
    return \$System->{$method->name}();
    }
    PHP;

    eval($php);
    }

    $System = new A();

    echo 'this was the result of ' . do_a();
    echo 'this was the result of ' . do_b();


    Please also note that if your methods use arguments, things get even more hairy. Also, if you name any of your methods the same as a function in the global namespace (ex. substr()), this will attempt to redefine them, and you'll probably get a Fatal Error.

    ReplyDelete
  3. You'll have to use a closure. Note that it's calling directly from the class definition, not the object:

    class test {
    function method() {
    echo 'method was called';
    }
    }

    $method = function(){call_user_func('test::method');};
    $method();
    $method();
    $method();

    //output:
    //method was calledmethod was calledmethod was called


    To call the method from the object, rather than the class, you'll have to pass the object into the closure:

    class test {
    var $count = 0;
    function method() {
    $this->count++;
    echo $this->count . "|<br />";
    }
    }

    $obj = new test;
    $obj2 = new test;
    $method = function($object){call_user_func(array($object, 'method'));};
    $method($obj);
    $method($obj);
    $method($obj);
    $method($obj2);
    //output:
    //1|
    //2|
    //3|
    //1|


    But that's not any prettier or simpler, is it?

    If you don't want to clutter up your page, just name the object something short:

    $pco = new page_controller_object_with_a_long_name_that_is_annoying;
    $pco->do_a();
    $pco->do_b();
    //etc.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.