Jump to content

Hook throwing an error

Featured Replies

Posted

So, it's been a while for me since i've done some plugins, now i'm trying getting back to it.. I've created plugin, for this plugin i've created a hook file, and no matter what i put inside of it, every time it throws error 

Quote

ParseError thrown with message "syntax error, unexpected '$variable' (T_VARIABLE), expecting function (T_FUNCTION) or const (T_CONST)"

that's how my hook file looks like:

//<?php

/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
	exit;
}

class hook28 extends _HOOK_CLASS_
{

$variable = "value";

}

 

You need a function in your hook.

  • Author

What kind of?

Or you use public or private $variable = “something”; or you create a function and call $variable inside. 

You should specify visibility. public, protected or private

class hook28 extends _HOOK_CLASS_
{
	public $variable = "value";
}

 

  • Author

@newbie LAC I did what you told me, now it throws http error 500

Show your code

  • Author
/* To prevent PHP errors (extending class does not exist) revealing path */
if ( !defined( '\IPS\SUITE_UNIQUE_KEY' ) )
{
	exit;
}

class hook28 extends _HOOK_CLASS_
{

public $tvc_conn = \IPS\Db::i('tvcdb', array('sql_host' => 'test', 'sql_user' => 'user', 'sql_pass' => 'pass', 'sql_database' => 'dbname'));

}

PHP doesn’t work this way, you can’t call a query here, this will work only inside a method

  • Author

Well, i never played with code hooks, can someone guide me on how I'm supposed to do it right way? I mean setting up db connection. 

This isn't really related to hooks....you have a class there, and with a class you can have methods or properties. You cannot, however, execute code such as a db query when attempting to set a property value.

http://php.net/manual/en/language.oop5.php

If you can tell us what you're trying to achieve and which class you have hooked on to we can probably provide a little more guidance.

  • Author

In developer center I've put \IPS\Db as extending class and I'm trying to set up only db connection on this hook.

What exactly are you trying to achieve? 

  • Author

I'm trying to set up external database connection..

If you are trying to achieve a connection to a DB, other than your community DB, you wouldn't overload the DB class...

Simply initialize the connection / create the object when you need it. Don't try to have it always persistent. 

Just create another DB class instance ?!  Take a look at DB::i() and the method parameters 🙂 

 

  • Author

I'm thinking that I don't get something. But here's what I did, I've created a public function inside hook class called dbconn, I've set up db connection there, website is not throwing any errors but I'm not sure if it's actually working..

8 hours ago, ~Jakso.` said:

I'm thinking that I don't get something. But here's what I did, I've created a public function inside hook class called dbconn, I've set up db connection there, website is not throwing any errors but I'm not sure if it's actually working..

If I understand what you're trying to do, it won't work like you expect it will. Adding in a public method to IPS\Db with your remote connection details there. Not going to work as you expect.. 

Don't hook into IPS\Db, hook into where you want the data and simply consolidate a method in you app / plugin that returns a DB instance connected to your remote  DB. Look at the docs I linked above that show how to set up the DB instance to an external DB as it is intended.

  • Author

Thing is, I don't want to hook into something, I just want to set up db connection so I can use it in other plugins..

So why not just use a second DB instance?  

	/**
	 * Get instance
	 *
	 * @param	mixed	$identifier			Identifier
	 * @param	array	$connectionSettings	Connection settings (use when initiating a new connection)
	 * @return	\IPS\Db
	 */
	public static function i( $identifier=NULL, $connectionSettings=array() )

Use a new identifier and pass the connection settings as second parameter

  • Author

I'm doing so. I've set up a public function called dbconn where I've put 

	$tvc_conn = \IPS\Db::i('tvcdb', array('sql_host' => 'test', 'sql_user' => 'user', 'sql_pass' => 'pass', 'sql_database' => 'dbname'));
	return $tvc_conn;
	

It should work, yes?

Yes

  • Author

Well then I guess the problem was solved..

Archived

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

Recently Browsing 0

  • No registered users viewing this page.