szalik.dev Posted September 27, 2018 Posted September 27, 2018 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"; }
Adriano Faria Posted September 27, 2018 Posted September 27, 2018 Or you use public or private $variable = “something”; or you create a function and call $variable inside.
newbie LAC Posted September 28, 2018 Posted September 28, 2018 You should specify visibility. public, protected or private class hook28 extends _HOOK_CLASS_ { public $variable = "value"; }
szalik.dev Posted September 28, 2018 Author Posted September 28, 2018 @newbie LAC I did what you told me, now it throws http error 500
szalik.dev Posted September 28, 2018 Author Posted September 28, 2018 /* 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')); }
Daniel F Posted September 28, 2018 Posted September 28, 2018 PHP doesn’t work this way, you can’t call a query here, this will work only inside a method
szalik.dev Posted September 28, 2018 Author Posted September 28, 2018 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.
bfarber Posted September 28, 2018 Posted September 28, 2018 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.
szalik.dev Posted September 28, 2018 Author Posted September 28, 2018 In developer center I've put \IPS\Db as extending class and I'm trying to set up only db connection on this hook.
szalik.dev Posted September 28, 2018 Author Posted September 28, 2018 I'm trying to set up external database connection..
Aiwa Posted September 28, 2018 Posted September 28, 2018 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.
Daniel F Posted September 28, 2018 Posted September 28, 2018 Just create another DB class instance ?! Take a look at DB::i() and the method parameters 🙂
szalik.dev Posted September 28, 2018 Author Posted September 28, 2018 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..
Aiwa Posted September 29, 2018 Posted September 29, 2018 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.
szalik.dev Posted October 3, 2018 Author Posted October 3, 2018 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..
Daniel F Posted October 3, 2018 Posted October 3, 2018 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
szalik.dev Posted October 3, 2018 Author Posted October 3, 2018 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?
szalik.dev Posted October 3, 2018 Author Posted October 3, 2018 Well then I guess the problem was solved..
Recommended Posts
Archived
This topic is now archived and is closed to further replies.