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
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.