Jump to content

Azarian

Members
  • Posts

    69
  • Joined

  • Last visited

Everything posted by Azarian

  1. I figured out what i was doing wrong print_r shows that it is working correct. Didn't realaize echo wouldn't echo out the whole array.
  2. Lets say we have an multi array. $somearray = array( 'config1' =>( 'seting1' => 'data', 'seting2' => 'data', 'seting3' => 'data', 'seting4' => 'data' ), 'config2' =>( 'seting1' => 'data', 'seting2' => 'data', 'seting3' => 'data', 'seting4' => 'data' ), ); Whats the best way to pull a sub array out to its own array? If I do this.... $new_array = $somearray['config2']; When I echo $new_array it returns array but no data fields. What am I missing here?
  3. So if I am understanding this, on my DB class if i delete the singleton setup and change my construct to something more like this? This is more like what I should be doing? public function __construct($db_settings = array()) { $db_settings['db_type']=$db_type; $db_settings['hostname'] =$hostname; $db_settings['username'] =$username; $db_settings['password'] =$password; $db_settings['db_name'] =$db_name; $db_settings['db_options'] =$db_options; try { $this->_pdo = new PDO( . $db_type . ':host=' . $hostname . ';dbname=' . $db_name, $username, $password, $db_options); } catch(PDOException $e) { $this->error = $e->getMessage(); } }
  4. I have never heard about this dependency injection or singleton being the anti pattern. I been googleing this for the last hour or two, to figure out what you meant and found it intriguing. Why is this singleton pattern for your DB class pretty much the standard that is plastered all over the internet? This dependency injection seems closer to the concept that I was using before I tried to go OOP with my code. With all these singleton examples you see everyone putting there CRUD or query building in the same class as the db connection. With dependency injection it looks like the DB class is for connection only and nobody is really using any kind of query building there just using the full blown code where ever they need it ?
  5. Looking for opinions if I am on the right track with my coding and how i can improve what I have so far. A little back ground first. I have a couple of applications I made back in the day in php, before php really followed OOP. A about 2 years ago i tried learning OOP practices to update a couple of my projects because a buddy of mine wanted to use them and for what ever reason my brain wasn't gettin it so I kinda pushed all my coding projects aside and haven't looked at them since. I got the coding bug again and I want to get some of these old projects redone so i can move on the bigger and better ideas I have in mind. I think what made my brain fry last time I tried to grasp this concept was i spent to much time reading different coders ideas on how to make perfect code than reading the next guy's and what he had to say was completely different than the next if you know what I mean. After looking at many examples of database classes this is what i came up with so far. There was a lot of code that was more elegant and more verastile but I opted for a more simplistic approach. class DB { private static $_instance = null; private $_pdo; private $_query; private $_error = false; private $_results; private $_count = 0; private $_db_options = array( PDO::ATTR_PERSISTENT => true, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION ); private function __construct() { try { $this->_pdo = new PDO('mysql:host=' . Config::get('database/hostname') . ';dbname=' . Config::get('database/db_name'), Config::get('database/username'), Config::get('database/password'), $this->_db_options); } catch(PDOException $e) { die($e->getMessage()); } } public static function getInstance() { if(!isset(self::$_instance)) { self::$_instance = new DB(); } return self::$_instance; } private function bind($bind) { foreach($bind as $param => $value){ $this->_query->bindValue($param, $value); } } public function select($column, $table, $where, $bind) { $sql="SELECT {$column} FROM {$table} WHERE {$where}"; $this->_query = $this->_pdo->prepare($sql); $this->bind($bind); return $this->_query; } public function selectall($table, $where, $bind) { $sql="SELECT * FROM {$table} WHERE {$where}"; $this->_query = $this->_pdo->prepare($sql); $this->bind($bind); return $this->_query; } public function delete($table, $where, $bind) { $sql="DELETE FROM {$table} WHERE {$where}"; $this->_query = $this->_pdo->prepare($sql); $this->bind($bind); return $this->_query; } public function insert($table, $fields, $values) { $sql="INSERT INTO {$table} ({$fields}) VALUES ({$values})"; $this->_query = $this->_pdo->prepare($sql); $this->bind($bind); return $this->_query; } public function update($table, $fields, $values) { $sql="UPDATE {$table} SET({$fields}) WHERE ({$values})"; $this->_query = $this->_pdo->prepare($sql); $this->bind($bind); return $this->_query; } public function resultset() { $this->_query->execute(); return $this->_query->fetchAll(PDO::FETCH_ASSOC); } public function result() { $this->_query->execute(); return $this->_query->fetch(PDO::FETCH_ASSOC); } public function rowCount() { return $this->_query->rowCount(); } public function lastInsertId() { return $this->_pdo->lastInsertId(); } } Now its how I am using it is where i am unsure if the approach i choose is the right way of going about it or not. This next class i am calling User_DataManager class its function is provide the User class Data from the Database. I don't want my User class to have any knownledge of the database's goings on. That way the only place I need to change that info will be in this class only. class User_DataManager { private $_db = null; public function __construct() { $this->_db = DB::getInstance(); } public function existing_emailaddress($email){ $table = 'tbl_users'; $where = 'fld_emailaddress = :email'; $bind = array(':email' => $email); $this->_db->selectall($table, $where, $bind); return $this->_db->result(); } public function banned_emailaddress($email){ $table = 'tbl_banned_emailaddresses'; $where = 'fld_emailaddress = :email'; $bind = array(':email' => $email); $this->_db->selectall($table, $where, $bind); return $this->_db->result(); } public function existing_username($username){ $table = 'tbl_users'; $where = 'fld_username = :username'; $bind = array(':username' => $username); $this->_db->selectall($table, $where, $bind); return $this->_db->result(); } public function banned_username($username){ $table = 'tbl_banned_usernames'; $where = 'fld_banned_username = :username'; $bind = array(':username' => $username); $this->_db->selectall($table, $where, $bind); return $this->_db->result(); } } To me this seeems like a lot of redudant code and this is just a small part of what is going on. Am I on the right track? Is there a better way I can or should be doing this?
  6. Is there some sort of written guidelines to this or is this where patterns come into play?
  7. I been thinking this over for the last few days and I think the design of my classes is flawed. I think I am having them do to much. With classes our end game with them is to create an object. What should the scope of the object be? Are we trying to make every piece of data a object or can an object be a whole dataset? Lets say for example were dealing with a login system and we have a username and pass. What would the best way to go about this to have a class that deals with the login and returns back if it was a success or not? Do we break it down farther and have a class for the username, class for pass, and a class that check.
  8. I am not trying to put one function into a class.
  9. Lets take the questions in a different direction once and maybe some of these smaller questions will help me solve the bigger question on my own than. What kinda of data is acceptable to pass to the class versus to the method? Which is the proper way of doing this? Example 1 $class = new SomeClass($data); $class->some_method(); or Example 2 $class = new SomeClass(); $class->some_method($data); In example 1 your passing the data to the construct to be setup for the entire class to use. Than you still have to call the method that processes the data. Example 2 you pass the data directly to the method but you have other supporting methods in that class that need that data too. Maybe I am not getting when to use and not to use the __construct method or using it improperly?
  10. Maybe your including something that is overriding some css and that is why both sources look the same?
  11. I get all that, it really doesn't answer my question though. If all the methods are meant to work with that data only why the extra step why can't it be automated so to speak?
  12. Why is the proper way to call a class is to call a method at the same time? For example $class = new SomeClass(); $class->some_method(); I understand why you would do this but lets say you just have a simple class that you pass some data. The class than processes the data in some way than returns it. It is my understanding if you were to make a class likes this. class SomeClass{ function __construct($data){ //process data// return $data; } } That this code is considered an improper way of doing this because you should never return data from the __construct. So would this follow code be the proper way to handle this? class SomeClass{ function __construct($data){ $this->some_method($data); } function some_method($data){ //process data// return $data; } } Any good explanation on the proper way to handle this would be greatly appreciated. When I am making simple classes to do a specific task it would seem more efficient not having to know about any particular method inside the class.
  13. I went back and looked at your code trying to figure out if I could make some combo of your code and mine work. I totally missed the fact you used array_keys() on both arrays. While Looking up to make sure that function did what I thought it did, I realized the in_array() I used didn't do what I thought it did. With a little bit of tweakin I got the code to work. Thanks for giving me the nudge I needed to figure this out! Here is the code that works. public function form_field_validation($form_array){ $postkeys = array_keys($form_array); foreach ($postkeys as $key){ if(array_key_exists($key, $this->required_fields)){ $field_ui_name = $this->required_fields[$key]; $user_input = $form_array[$key]; if(!$user_input){ array_push($this->validation_fail_msg, " $field_ui_name is a required field."); } } } }
  14. Your kinda on the right track. Your code only gets me half way there though. Let me break this down psuedo style and try to explain a little better. $_post = contains the form field names => with the user submited value $my_preset_var = List of all possible form field names => What the user read on the form for example a form field maybe named email_address but on UI side the user sees Email Address. Not all the possible field names will ever be passed to the function at the same time. Lets say at some point else where in my code I want to check if an email field was filled in by a user. I pass it to the function. It sees email_address is a valid field to be passing to the function. Than it checks if the user filled in the field. If the user didn't fill in the field it returns back Email Address is a required field. I hope this break down is more clear.
  15. I have a class I wrote that handles the back end processing for registering/login/change pass/pass recovery. What I am trying to do is basically pass the $_POST var to a function than run through the values to see if they are set. I am trying to compare the $_POST array to a preset array with valid form field names with also the names the user read on the page, so it can be returned to the user which field or fields they messed up. I can't seem to wrap my head around how to make this work. Anyone have any thoughts or suggestions what I can do? private $required_fields = array( 'user_name' =>'Username', 'email_address' => 'Email Address', 'password' => 'Password', 'password2' => 'Confirm Password', 'oldpassword' => 'Old Password', 'newpassword' => 'New Password', 'newpassword2' => 'Confirm New Password' ); public function form_field_validation($form_array){ foreach ($form_array as $key){ $field = $form_array[$key]; if(in_array($field, $this->required_fields)){ $field_name = $this->required_fields[$field]; if(!$field){ array_push($this->validation_fail_msg, " $field_name is a required field."); echo $this->validation_fail_msg; } } } }
  16. Ah! Ok you weren't that clear before about the URL that is the issue. This code works now. function main_page(){ if (!isset($_GET['page']) || empty($_GET['page'])) { $menu_link = action_welcome_page; $menu_link(); }else{ $menu_link = 'action_' . ucfirst($_GET['page']); if (is_callable($menu_link)) { $menu_link(); } } }
  17. I can read it a million types still not going to make the code work properly!
  18. You welcome page would be called whenever someone accesses your site without ?page being tacked on the end. You could over course re-write this to handle an empty ?page= if (!isset($_GET['page']) || empty($_GET['page'])) { Not if (like I said) all your functions are prefixed with action* as in.... actionFoo() {} actionBar() {} This is how most major frameworks handle the exact same issue, though they also have these functions contained within controller classes. When I renamed all the functions prefixed with action. function action_somefunction(){} When that code would execute it would return this. action_Action_somefunction This isn't what we want.
  19. I understand what you are trying to do but this code won't work at all. I think I will have to think about it awhile and rewrite this function differently. If I use !isset instead of checking if $_GET is null or not it will never load the welcome_page function. This code 'action' . ucfirst($_GET['page']) is nice for stopping built it functions from being executed but it also stops my own function from being executed as well. Actually if we did a check first to see the function name was already prefixed with action this would be a better way wouldn't it?
  20. Sure, get_defined_functions (in particular the "user" array returned from that; unless I missed your point entirely ). Yea actually according to the description that is totally what I was asking for but looking at the examples i am kinda lost how to use it. LOL
  21. Well I am only particailly right it seems. Removing all the prefixes isn't the answer either. This code doesn't see my functions than. $menu_link = 'action_' . $_GET['page']; if (is_callable($menu_link)) { $menu_link(); } To make it all work properly this is the code that would have to be used whether or not I prefix all the functions. Which brings us back to where I already was basically. $menu_link = $_GET['page']; if (is_callable($menu_link)) { $menu_link(); }
  22. If I prefix all my functions with action_ for example. $menu_link = 'action_' . ucfirst($_GET['page']); This line of code wouldn't make any sense. If my function name is action_somefunction this code would really make it action_Action_somefunction. Although if I understand correctly what we are trying to accomplish. I shouldn't prefix any of my functions with anything and use this code. $menu_link = 'action_' . $_GET['page']; if (is_callable($menu_link)) { $menu_link(); } I don't need the function to be uppercased. This is where the whole is_callable would finally make some sense now. If someone tried to call a built in function it would fail because action_builtinfunction would never exist. Right?
  23. Yea I already looked it up on that page and it doesn't answer my question.
  24. Ok I like the action example better! When the is_callable function is called what is it testing the variable against to know if it is valid funtion or not?
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.