Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

Archive

Our old forums are still available as a read-only archive.

Moderators: martimiz, Sean, biapar, Willr, Ingo, simon_w

Custom Function but parameter is always NULL


Go to End
Reply

10 Posts   3153 Views

Avatar
ElBarto

12 June 2008 at 7:29pm Community Member, 3 Posts

I want to have a function that generates me a copyright year range.
I call it in the footer like this: $CopyrightRange(2007)
Then it should print "2007-2008"
But the parameter is always NULL. I checked this with the var_dump you can see below.
If have tested this function in the Page class and in the Page_Controller class but it doesn't work at all.
What goes wrong here??

public function getCopyrightRange($from) {
$to = date('Y');
var_dump($from);
if(!empty($from) && $from < $to) {
return $from . '&ndash;' . $to;
}
return $to;
}

Avatar
dio5

12 June 2008 at 7:57pm Community Member, 501 Posts

You can also do just

2007 - $Now.Year

in the template

Avatar
ElBarto

12 June 2008 at 8:04pm Community Member, 3 Posts

I could but that's not what I want.
Ich want do write $CopyrightRange(2008)
and in 2009 in should then automatically print 2008-2009
but this year it should print only 2008

Avatar
dio5

12 June 2008 at 8:13pm Community Member, 501 Posts

the problem is the 'get' in getCopyrightRange($from)

Either add the 'get' in the template, or get rid of it in the controller/model.. whereever you've put it.

Avatar
Sean

12 June 2008 at 8:15pm Forum Moderator, 921 Posts

This is caused by a deficiency in the templating language.

You either have to:

1. Change getCopyrightRange() to CopyrightRange() in your PHP code (removing the "get" prefix)
2. OR, you can call $getCopyrightRange(2007)

Sean

Avatar
Sean

12 June 2008 at 8:16pm Forum Moderator, 921 Posts

dio5: d'oh! Posted same time as you! ;-)

Avatar
ElBarto

12 June 2008 at 8:43pm Community Member, 3 Posts

Ok thanks.
Can you explain why sometimes I have to use getFooBar and sometime only FooBar is enough?

Avatar
Sean

12 June 2008 at 10:56pm Forum Moderator, 921 Posts

It only happens when there's an argument to a function call.

For example:

function getPage($id) {
return DataObject::get_by_id('Page', (int)$id);
}

Using $Page(5) in the template will call ->getPage() and not pass in the parameter, which will cause a problem if you're assumption is a parameter that should always be there.

However, calling $getPage(5) in the template will call ->getPage(5) passing in the parameter correctly.

Perhaps it is indeed a bug, but it doesn't appear to be a major one, as it's easily worked around in the templates.

Go to Top