Jump to content

Redeclaring a method I defined


bl00dshooter

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/219245-redeclaring-a-method-i-defined/
Share on other sites

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?

 

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.