bl00dshooter Posted November 19, 2010 Share Posted November 19, 2010 Hello folks, So, I have decided to start a small CMS, for learning purposes. It's going pretty smooth, but I decided to implement a hook system, so you can extend the CMS without touching the core files. I have the following idea: To create a hook, a person creates a php file in the hooks folder, and simply redeclare every method of my classes he/she wants. Example: I have a class called Users that has a method called showUser, like the following: class Users { function showUser($id) { echo "Welcome ". $id; } } then, in the hooks folder, a file called Users_leavePlease.php: function showUser($id) { echo "Please leave " . $id; } The problem is...php won't allow it. It gives me the function redeclare error if I include the php file. Any ideas how can I make this work? thanks, bl00dshooter. Quote Link to comment https://forums.phpfreaks.com/topic/219245-redeclaring-a-method-i-defined/ Share on other sites More sharing options...
snowman15 Posted November 19, 2010 Share Posted November 19, 2010 I'm pretty sure if you put that function in a class and then made it a private function to that class, it might not give you that error. Please expand on what your trying to do. Something tells me you shouldn't be structuring it like this anyway. Quote Link to comment https://forums.phpfreaks.com/topic/219245-redeclaring-a-method-i-defined/#findComment-1136954 Share on other sites More sharing options...
bl00dshooter Posted November 19, 2010 Author Share Posted November 19, 2010 The whole point of this was that this hook file function should be used over the core function, if it exists. Well, that was the better idea I could come up with. Do you have a better suggestion of structuration of a hook system, that I can implement? Quote Link to comment https://forums.phpfreaks.com/topic/219245-redeclaring-a-method-i-defined/#findComment-1136965 Share on other sites More sharing options...
ManiacDan Posted November 19, 2010 Share Posted November 19, 2010 Sub-classes override their parents. class a { function doSomething() { echo "Hello!"; } } class b extends a { function doSomething() { echo "Goodbye!"; } } $class = new b(); $class->doSomething(); //echoes Goodbye! Now, take that theory and look into the factory pattern, which allows your code to dynamically decide which class to use. -Dan Quote Link to comment https://forums.phpfreaks.com/topic/219245-redeclaring-a-method-i-defined/#findComment-1136972 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.