Invision Community 4: SEO, prepare for v5 and dormant account notifications Matt November 11, 2024Nov 11
Posted September 27, 20186 yr 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"; }
September 27, 20186 yr Or you use public or private $variable = “something”; or you create a function and call $variable inside.
September 28, 20186 yr You should specify visibility. public, protected or private class hook28 extends _HOOK_CLASS_ { public $variable = "value"; }
September 28, 20186 yr 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')); }
September 28, 20186 yr PHP doesn’t work this way, you can’t call a query here, this will work only inside a method
September 28, 20186 yr 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.
September 28, 20186 yr 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.
September 28, 20186 yr 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.
September 28, 20186 yr 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.
September 28, 20186 yr Just create another DB class instance ?! Take a look at DB::i() and the method parameters 🙂
September 28, 20186 yr 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..
September 29, 20186 yr 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.
October 3, 20186 yr 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..
October 3, 20186 yr 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
October 3, 20186 yr 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?
Archived
This topic is now archived and is closed to further replies.