iversonm Posted July 25, 2010 Share Posted July 25, 2010 class Settings_Core { private function _Global_Settings(){ global $MySQL; $Query = 'Select `Name`, `Value` From `Site_Settings`.`Global` Where `Active` = 1 Order By `Id` ASC'; $Result = $MySQL->query($Query, MYSQLI_USE_RESULT); while($Data = mysqli_fetch_assoc($Result)){ $this->Global[$Data['Name']] = $Data['Value']; } } }; error is Notice: Indirect modification of overloaded property Settings_Core::$Global has no effect in /var/www/Include/Core/Settings.Class.php on line 28 I have no idea what to do or what this means or how to fix it Quote Link to comment https://forums.phpfreaks.com/topic/208866-overloading/ Share on other sites More sharing options...
trq Posted July 25, 2010 Share Posted July 25, 2010 Can we see all of the code in this class plus any code where you are calling it? Quote Link to comment https://forums.phpfreaks.com/topic/208866-overloading/#findComment-1091039 Share on other sites More sharing options...
iversonm Posted July 25, 2010 Author Share Posted July 25, 2010 here is Settings.Core.php <?Php class Settings_Core { public function __construct(){ $this->_Global_Settings(); $this->_Load_Secondary_Tables(); } private function _Global_Settings(){ global $MySQL; $Query = 'Select `Name`, `Value` From `Site_Settings`.`Global` Where `Active` = 1 Order By `Id` ASC'; $Result = $MySQL->query($Query, MYSQLI_USE_RESULT); while($Data = mysqli_fetch_assoc($Result)){ $this->Global[$Data['Name']] = $Data['Value']; } } private function _Load_Secondary_Tables(){ global $MySQL; if($this->Global['Load_Setting_Tables']){ $Query = 'Select `Table_Name` Where `Active` = 1'; $Result = $MySQL->query($Query, MYSQLI_USE_RESULT); while($Data = mysqli_fetch_assoc($Result)){ $New_Query = 'Select `Name`, `Value` From `Site_Settings`.`'.$Data['Table_Name'].'` Where `Active` = 1'; $Rules = $this->DB->query($New_Query, MYSQLI_USE_RESULT); while($New_Data = mysqli_fetch_assoc($Rules)){ $this->$Data['Table_Name'][$New_Data['Name']] = $New_Data['Values']; } } } } }; this is the first file that calls the settings class class Core { public function __construct(){ error_reporting(E_ALL); $this->_Start_Connections(); $this->_Start_Settings_Core(); $this->_Get_URL_Contents(true); } private function _Start_Connections(){ $this->Memcache = $GLOBALS['Memcache'] = new Memcache; $this->Memcache->connect('haha', 11211) or die('could not connect to memcache'); $this->DB = $GLOBALS['MySQL'] = new mysqli('ip', 'usr_re_hosting', 'ya right') or die('could not connect to mysqli'); } private function _Start_Settings_Core(){ require_once('Settings.Class.php'); $GLOBALS['Settings'] = $this->Settings_Core = new Settings_Core; if(class_exists('Settings_Core')){ return true; }else{ return false; } } private function _Start_Sessions(){ session_cache_expire($this->Settings_Core->Global['Session_Expire']); if(!session_start()){ die('Session Failed to start'); } } private function _Get_URL_Contents($Set_Defaults){ $Contents = array('Function'=>'Home', 'Game'=>false, 'Site'=>false, 'Amount'=>false, 'Faction'=>'Horde', 'Server'=>false); foreach($Contents as $Key=>$Value){ if(isset($_GET[$Key]) && $_GET[$Key]!==NULL){ $this->$Key = $_GET[$Key]; }else{ if($Set_Defaults == true){ $this->$Key = $Value; } } } } public function Get_Site_Updates($Limit = 10){ $Query = 'SELECT `Time`,`Who`,`Email`, `Title`, `Entry` FROM `Site_Info`.`Main_Updates` Order By `Time` DESC Limit '.$Limit; $Result = $this->DB->query($Query, MYSQLI_USE_RESULT); while($Data = mysqli_fetch_assoc($Result)){ $this->Global_Updates[] = $Data; } } }; $Core = new Core; Quote Link to comment https://forums.phpfreaks.com/topic/208866-overloading/#findComment-1091047 Share on other sites More sharing options...
trq Posted July 26, 2010 Share Posted July 26, 2010 With hat many globals kicking around you may as well drop the entire idea of using classes anyways. Quote Link to comment https://forums.phpfreaks.com/topic/208866-overloading/#findComment-1091068 Share on other sites More sharing options...
iversonm Posted July 26, 2010 Author Share Posted July 26, 2010 well what can i do instead with the format? this is a learning process for me and i am not that best at oo structure but i don't want to drop the classes.. and that answer doesn't solve my problem for the overloading Quote Link to comment https://forums.phpfreaks.com/topic/208866-overloading/#findComment-1091072 Share on other sites More sharing options...
trq Posted July 26, 2010 Share Posted July 26, 2010 The error relates to the fact that you are storing data within a variable: $this->Global[$Data['Name']] which has not been declared within the class definition. Having said that. This entire design is shot. Classes should one entity within your system. Creating a class called 'Core' and jamming misc functionality within it is NOT oop. Then, there is the issue of globals. Global data breaks your objects encapsulation because they are now relying on outside data suddenly turning up. If you need to get data into your objects, force it to be provided through a proper interface. Quote Link to comment https://forums.phpfreaks.com/topic/208866-overloading/#findComment-1091073 Share on other sites More sharing options...
iversonm Posted July 26, 2010 Author Share Posted July 26, 2010 im not trying to ask for you to write my script for me, but i will ask if you can show me an example or point in me in the right direction with a tut. iv been looking up proper oop structure but i dont quite understand how i would make what im trying to do fit into that structure. so to give you an example the global is my mysqli functions. would it make sense to extend the mysqli class to all the classes or am i missing the concept of oop as a whole Quote Link to comment https://forums.phpfreaks.com/topic/208866-overloading/#findComment-1091079 Share on other sites More sharing options...
trq Posted July 26, 2010 Share Posted July 26, 2010 Just because one object may rely upon the functionality of another does not mean it should be an extension of that other object. For example, an Authentication class may need to use a database class in order to lookup username/password combinations within a database, does it extend the database classes functionality though? No, it simply uses it. As for your global situation and your database object. Any class that requires the ability to query the database should be passed the database object either through it __construct, or some other method. In order to make this as flexable as possible, your database object should also implement some 'interface' so that your objects that are going to use it understand exactly what your database object provides. A very simple example (not tested and no error handling in place). <?php interface Db { public function connect($host, $user, $pass, $db); public function query($sql); } class Mysql implements Db { private $conn; public function connect($host, $user, $pass, $db) { $conn = mysql_connect($host, $user, $pass); mysql_select_db($db); $this->conn = $conn; } public function query($sql) { return mysql_query($sql); } } class SomeClassThatNeedsAccessToADb { private $db; public function __construct(Db $db) { $this->db = $db; } public function getAllUsers() { $this->db->connect(); return $this->db->query(" SELECT * FROM Users "); } } $obj = new SomeClassThatNeedsAccessToADb(new MySql('foo','foo','foo','foo')); $result = $obj->getAllUsers(); Doing things this way it is easy to change databases because your objects rely on a defined interface. eg; SomeClassThatNeedsAccessToADb relies on being passed an object that implements the Db interface. Because of this, it is always safe to use the connect() and query() methods this interface provides. You could easily build a database class now that connects to an sqlite database, as long as it implements the same Db interface your application won't break. This is by no means a perfect example, but it hopefully gets accross the idea. Quote Link to comment https://forums.phpfreaks.com/topic/208866-overloading/#findComment-1091087 Share on other sites More sharing options...
iversonm Posted July 26, 2010 Author Share Posted July 26, 2010 my other quick question for you before i really start getting into this is what is the point of a static method vs non static. what advantages do you have in declaring something static? Quote Link to comment https://forums.phpfreaks.com/topic/208866-overloading/#findComment-1091089 Share on other sites More sharing options...
iversonm Posted July 26, 2010 Author Share Posted July 26, 2010 so a couple quick questions, whats the point of inside the SomeClassThatNeedsAccessToADb class inside construct function having Db before $db does that implement the interface. and i still understand how the interface helps if you know you have the connect and query function inside the db class i really do appreciate your help Quote Link to comment https://forums.phpfreaks.com/topic/208866-overloading/#findComment-1091093 Share on other sites More sharing options...
trq Posted July 26, 2010 Share Posted July 26, 2010 so a couple quick questions, whats the point of inside the SomeClassThatNeedsAccessToADb class inside construct function having Db before $db does that implement the interface. This forces the __construct method to accept only an object of type 'Db'. and i still understand how the interface helps if you know you have the connect and query function inside the db class I'll assume you meant you dont understand. By forcing your code to implement interfaces you define what objects can and can't accept. This makes your code allot more reliable and in the long run, more flexible. Quote Link to comment https://forums.phpfreaks.com/topic/208866-overloading/#findComment-1091094 Share on other sites More sharing options...
trq Posted July 26, 2010 Share Posted July 26, 2010 For instance. Say I am using an sqlite database, but I wont to use your SomeClassThatNeedsAccessToADb in my project. I know it requires a database object that implements the 'Db' interface, so all I have to do is create an sqlite class that implements the same interface and It is guaranteed to work with your SomeClassThatNeedsAccessToADb without me ever having to edit its contents. A big point of OOP is that classes should never need to be edited to be used. Quote Link to comment https://forums.phpfreaks.com/topic/208866-overloading/#findComment-1091095 Share on other sites More sharing options...
iversonm Posted July 26, 2010 Author Share Posted July 26, 2010 yes you were right i meant i dont understand. ok so the idea of an interface is for uniformity. so a small example lets see i had a db classs that utilizes mysql then i made a new class that utilizes mssql for whatever the reason would be. the idea of the interface is to make sure its all the same and the class implementing the db1 or db2 class does not have to changed depending on which database im using if that makes sense? also unless i missed it why would you use static methods and then last for what i had inside my class functions to load things and do that stuff, should i just have that stuff run outside of my classes in a Procedural method or should i make a more classes for them? Quote Link to comment https://forums.phpfreaks.com/topic/208866-overloading/#findComment-1091103 Share on other sites More sharing options...
trq Posted July 26, 2010 Share Posted July 26, 2010 why would you use static methods Generally, class get instantiated into objects. These objects have methods that operate on data within the objects themselves. If, you have a class that doesn't contain its own set of data, there isn't really any need to instantiate it into an object. That's when you would use static methods. and then last for what i had inside my class functions to load things and do that stuff, should i just have that stuff run outside of my classes in a Procedural method or should i make a more classes for them? The problem with your Cor class for instance is it contains a bunch of unrelated methods that don't really share common data. A class should describe one (and one only) 'thing'. The code within 'Core' should likely be moved out into what is known as 'client code'. This is the code that call's upon objects to do its work. Some of the methods within 'Core' however could likely be turned into there own classes. Quote Link to comment https://forums.phpfreaks.com/topic/208866-overloading/#findComment-1091106 Share on other sites More sharing options...
iversonm Posted July 29, 2010 Author Share Posted July 29, 2010 ok so looking at your example makes perfect sense to me now, but my other question now would be if i was using the mysqli extension should i build my own functions to query and all that sort of stuff that will utilize Procedural style or should i extend mysql class with mysqli. to me it would make sense to do the Procedural style and make my own functions so i can make sure they are all universal that utilize an interface in case i were to switch to mssql or something else but i want to just confirm that i am thinking correctly for this oo stuff Quote Link to comment https://forums.phpfreaks.com/topic/208866-overloading/#findComment-1092413 Share on other sites More sharing options...
iversonm Posted July 29, 2010 Author Share Posted July 29, 2010 also other question, so if i have my proper interfaces and classes like memcache and mysql would it be proper to have a class for all user related functions? so in example registering, logging in, logging out? or is that not proper and should client code be in the form of a class or should it be procedural with no class? Quote Link to comment https://forums.phpfreaks.com/topic/208866-overloading/#findComment-1092425 Share on other sites More sharing options...
iversonm Posted July 29, 2010 Author Share Posted July 29, 2010 one more question. so lets say you have 5 websites you are pulling data from and there are 10 different things you are collecting data on each website, but all 5 websites all have these 10 things. if you need to get the last result, selected results in a timeframe and all results would it be better to make a class for each site that have the same functions that implements an interface or make 3 classes and each class will either get lsat result, selected results, or all results and have a function in each class for each of the 5 websites? does that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/208866-overloading/#findComment-1092433 Share on other sites More sharing options...
trq Posted July 29, 2010 Share Posted July 29, 2010 1) Yes, even using something like mysqli you should probably build your own wrapper around it based on a concrete interface. You actually design the interface first, and then make your actual database class implement that interface. Another of php's built in database objects (PDO) is something that you should look at. It actually provides all this for you. It can connect to multiple database types and provides the same interface to all of them. In this case, you don't really need to write your own interface. Of course to keep to your own class naming convention you might like to make a wrapper that simply extends the PDO object. 2) Yes its proper to have classes for specific functionality. You need to think carefully about what fits where however. A User for instance would simply be used to describe a User of your application. It might simply store infomation about the user. Logging in and out is likely the job of an authentication object or similar. I guess, this is the hardest part, deciding what goes where. Keep this in mind though. Classes should be as specific as possible. If you find yourself putting too much into one class, its likely it needs to be broken down into more. 3) Its generally fine to have client code programmed procedurally. Though after a while, you may find yourself with such a robust framework that even client logic ends up in classes. Don't try and force things into a class just for the hell of it though, you'll know what I mean (in the previous sentence) when you get there. Quote Link to comment https://forums.phpfreaks.com/topic/208866-overloading/#findComment-1092436 Share on other sites More sharing options...
iversonm Posted July 29, 2010 Author Share Posted July 29, 2010 ok thanks for answering my questions again i really appreciate your patience with me. I am learning a lot and I am sure anyone who is reading through this is learning a lot too. ill let you know if i come up with anything else Quote Link to comment https://forums.phpfreaks.com/topic/208866-overloading/#findComment-1092437 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.