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

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex