Skip to main content

Determining source of method call at runtime



I'm wondering if there is a "better" way to do this:







class Foo {

final public function bar() {

if (is_subclass_of(get_called_class(), __CLASS__)) {

throw new Exception();

}

}

}



class Bar extends Foo {

public function baz() {

parent::bar(); // shouldn't be allowed

}

}







Essentially, I want certain methods in my parent class to prohibit child classes from calling them. This needs to be bullet-proof, which I doubt this is, so if you know how this could be circumvented, that's what I'm interested in knowing (along with how to prevent it, if possible).





Edit: For everyone suggesting private methods, this is not an option, as I need the interface to remain public to be externally accessible. Sorry, I guess I assumed that would be obvious.


Comments

  1. class Foo {
    final public function bar() {
    if (is_subclass_of(get_called_class(), __CLASS__)) {
    throw new Exception('No cookies for you!');
    }
    echo 'Failure!';
    }
    }

    class Bar extends Foo {
    public function baz() {
    try{
    Foo::bar(); // shouldn't be allowed
    } catch (Exception $e){
    echo $e->getMessage();
    }
    try{
    $func = function() {Foo::bar();}; // is allowed, nags somewhat about it should't be called statically..
    $func();
    } catch (Exception $e){
    echo $e->getMessage();
    }
    }
    }
    $b = new Bar();
    $b->baz();

    ReplyDelete
  2. This is not going to be "bulletproof". And I don't think there's anything you can do to make it so.

    Fact of the matter is, a PHP process is usually running in an interpreter with filesystem access to the local server - as a relatively-unprivileged user. But that's still enough to open /proc/self/mem, which provides read-write access to the memory space of the current process. Using that, you could go into the memory the PHP interpreter is using and NOP over the bit of code you thought was providing your bulletproof security.

    ReplyDelete
  3. Im not sure if this helps, but private functions in parent classes cant be called by child classes. But if you need it to be public i think you would have to declare baz() in each child and throw an exception...

    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