Jump to content

OOP DateTime extension EASY question


Lodius2000

Recommended Posts

So I am extending the DateTime class and would like to completely disable the modify() function that is built in so i have this:

<?php
class NewDateTime extends DateTime
{

public function modify()
{
	throw new Exception('modify() has been disabled.');
}
}

 

thats not the entire class description, just the pertinent part of course

 

now any time I create a new object from NewDateTime I get this message at the top of the screen:

 

Strict Standards: Declaration of NewDateTime::modify() should be compatible with that of DateTime::modify() in C:\xampp\htdocs\.....my file path here.....\NewDateTime.php on line 62

 

what gives? how do I get rid of this and still disable modify()? I'm betting its a setting that i have wonky but I am really rusty in php so dont know where to look in php.ini

 

Link to comment
Share on other sites

I assume that the class implements an interface that defines modify and it's params. try:

 

 

public function modify($datetime = false) {
   throw new Exception('modify() has been disabled');
}

 

?

Link to comment
Share on other sites

Andy-H

That worked. just to make sure I understand how it worked (like i said, reeeeeeally rusty at my php): including the false value for $datetime means that modify() must accept some sort of argument, so if you put in an argument it returns false because it is supposed to throw an exception, and if you put no argument, the function returns false, which throws an exception

 

right?

Link to comment
Share on other sites

Far from correct.

 

Currently the modify() method expects at least one argument. Your overloaded method didn't that is why you where getting the error.

 

What Andy-H's code does is declares that your modify() also expects an argument, however he has also set a default value for this argument (of false) making it now optional.

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.