Call to a member function variable() on null - can you use $request with this event?

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
aster59
Registered User
Posts: 82
Joined: Sun Jun 26, 2016 1:47 pm
Location: United States

Call to a member function variable() on null - can you use $request with this event?

Post by aster59 » Sat Dec 30, 2017 9:16 pm

Hi everyone, happy holidays.

My goal is to:
Grab a query string (&ref) from the users url after they click agree to the terms.

Url ex.)
https://forum.com/ucp.php?mode=register&ref=person_who_ref_me

I used the event:
core.ucp_register_data_before

My code was inside my function in the listener:

Code: Select all

$ref = $request->variable('ref', '', false,\phpbb\request\request_interface::GET);
But I get the error in my title. I declared $request like this in my listener file.

Code: Select all

public function __construct(\phpbb\template\template $template,
							    \phpbb\controller\helper $helper,
							   	\phpbb\user $user,
							   	\phpbb\db\driver\driver_interface $db,
							   	\phpbb\request\request $request) 
What did I do? Can I not do request with this event or am I going about this the wrong way? Basically I need this query string as a referral to be in the database after the user registers and I think this is the first step...

User avatar
AbaddonOrmuz
Registered User
Posts: 694
Joined: Wed Dec 25, 2013 9:06 pm
Location: /dev/null
Name: Alfredo Ramos
Contact:

Re: Call to a member function variable() on null - can you use $request with this event?

Post by AbaddonOrmuz » Sat Dec 30, 2017 9:54 pm

Show the complete code, because you're not declaring $request, your code says that you're expecting that $request will be a instance of phpbb\request\request, but you're not assingning that variable so you can't use it outside the __construct() method.

Read about dependency injection:

https://area51.phpbb.com/docs/dev/3.2.x ... -injection
Some of my phpBB extensions: [ Imgur | SEO Metadata | Markdown ]
Check out all my extensions
Arch Linux user

aster59
Registered User
Posts: 82
Joined: Sun Jun 26, 2016 1:47 pm
Location: United States

Re: Call to a member function variable() on null - can you use $request with this event?

Post by aster59 » Sat Dec 30, 2017 10:01 pm

AbaddonOrmuz wrote:
Sat Dec 30, 2017 9:54 pm
Show the complete code, because you're not declaring $request, your code says that you're expecting that $request will be a instance of phpbb\request\request, but you're not assingning that variable so you can't use it outside the __construct() method.

Read about dependency injection:

https://area51.phpbb.com/docs/dev/3.2.x ... -injection
Oh no, I had forgotten $this but it didn't fully solve it. Here is the full code...
Now I get an error: Cannot access empty property. I have my namespace declared and using Symfony.

Code: Select all


class points_listener implements EventSubscriberInterface {
	
	protected $template, $helper, $user, $db, $request;
	
	public function __construct(\phpbb\template\template $template,
							    \phpbb\controller\helper $helper,
							   	\phpbb\user $user,
							   	\phpbb\db\driver\driver_interface $db,
							   	\phpbb\request\request $request) 
	{
		$this->template  = $template;
		$this->helper    = $helper;
		$this->user      = $user;
		$this->db        = $db;
		$this->request   = $request;
	} 
	
    static public function getSubscribedEvents()
    {
        return array(
	    'core.ucp_register_data_before'		   => 'ref_link_find',
        );
    }

    public function ref_link_find($event) {
    	
    	$ref = $this->$request->variable('ref', '', false,\phpbb\request\request_interface::GET);
    	die(print $ref);
    }

And just using die() to see the value but I get that error instead.

I also tried using SERVER and HTTP_REFERER but that is empty too?
Last edited by aster59 on Sat Dec 30, 2017 10:06 pm, edited 2 times in total.

User avatar
AbaddonOrmuz
Registered User
Posts: 694
Joined: Wed Dec 25, 2013 9:06 pm
Location: /dev/null
Name: Alfredo Ramos
Contact:

Re: Call to a member function variable() on null - can you use $request with this event?

Post by AbaddonOrmuz » Sat Dec 30, 2017 10:04 pm

You have a typo, it's not $this->$request() but $this->request()

You might wan't to use var_dump() or print_r() for debuggin.
Some of my phpBB extensions: [ Imgur | SEO Metadata | Markdown ]
Check out all my extensions
Arch Linux user

aster59
Registered User
Posts: 82
Joined: Sun Jun 26, 2016 1:47 pm
Location: United States

Re: Call to a member function variable() on null - can you use $request with this event?

Post by aster59 » Sat Dec 30, 2017 10:09 pm

AbaddonOrmuz wrote:
Sat Dec 30, 2017 10:04 pm
You have a typo, it's not $this->$request() but $this->request()

You might wan't to use var_dump() or print_r() for debuggin.
Oh, well how pathetic of me. Thanks. Sorry a little rusty.

Yeah, good idea.

User avatar
AbaddonOrmuz
Registered User
Posts: 694
Joined: Wed Dec 25, 2013 9:06 pm
Location: /dev/null
Name: Alfredo Ramos
Contact:

Re: Call to a member function variable() on null - can you use $request with this event?

Post by AbaddonOrmuz » Sat Dec 30, 2017 10:12 pm

Don't worry, we've all had one of those days :lol:
Some of my phpBB extensions: [ Imgur | SEO Metadata | Markdown ]
Check out all my extensions
Arch Linux user

Post Reply

Return to “Extension Writers Discussion”