Jump to content

NotionCommotion

Members
  • Posts

    2,446
  • Joined

  • Last visited

  • Days Won

    10

Everything posted by NotionCommotion

  1. Well, I don't know how deep, but thanks anyway. I am messing around with a database called influx which requires RFC3339 UTC time stamps in the WHERE clause. The user selects a duration such as 1 hour, 5 days, 3 years, etc. I then wish to calculate some aggregate value based on right now going back the given duration. I wish to store that duration in a SQL database so that I may do the query later. Thanks
  2. I don't care about start and finish, but only duration. How long is the TV show you watched yesterday, requinx? Ah, one hour! How long would it be if you watched it today? I actually do care about start and finish, but start is always exactly right now.
  3. I have an input field for time value and a select menu for the units (hours, days, weeks, months, or years) The application needs seconds (average is okay as not every month is equal, etc). I need to later be able to display the time duration that was entered (i.e. 18 months). Am I stuck storing the original value and the units?
  4. I think both of you do Perfect. Exceptions, validation, some URL manipulation "yikes" and "even more yikes" dynamic SQL.
  5. I can answer what I think is unambiguous, but I can't answer what the majority of PHP programmers consider proper, and I would like to be consistent with the greater majority (of good coders). Would you say the only time it is acceptable is when the variable has either white space or a double quote on either side? Furthermore, only simple variables and maybe objects, but not arrays?
  6. Hi Jacques1, I've already made a change to using it when arrays and objects are involved. I should have asked whether something like this is considered bad practice. If not, where should one draw the line? $color="brown"; $string="The $color cow jumped over the moon.";
  7. I agree it is easier to read and less prone to errors, and have recently made an effort of using it for anything in the slightest ambiguous. I am starting to think even that might be too lax, and leads to inconsistencies. Maybe a better question is whether you believe simple syntax should ever be used?
  8. I've read the manual http://php.net/manual/en/language.types.string.php#language.types.string.parsing, and know when it has to be used. When should it be used? For instance, is $url1 or $url2 considered better practice? Are their other common scenarios where complex syntax is not required, but should be used? $this->a='aaa'; $arr['c']='ccc'; $url1="$this->a/foo/$arr[c]/bar"; $url2="{$this->a}/foo/{$arr['c']}/bar";
  9. Dang, that must be some good hot chocolate! Does the Fibonacci algorithm have anything to do with what I am trying to accomplish, or is it just an example of recursive linear implementation?
  10. I have the following array of objects represented by the below JSON. [ {id: 123, type: "aggr", duration: 10, offset: 30, timestamp: 444, foo: "bar"}, {id: 223, type: "aggr", duration: 20, offset: 40, timestamp: 333, foo: "bar"}, {id: 323, type: "aggr", duration: 10, offset: 30, timestamp: 222, foo: "bar"}, {id: 523, type: "aggr", duration: 20, offset: 40, timestamp: 444, foo: "bar"}, ...... {id: 623, type: "aggr", duration: 20, offset: 30, timestamp: 444, foo: "bar"}, {id: 723, type: "aggr", duration: 10, offset: 30, timestamp: 666, foo: "bar"} ] I need to group them based on common duration and offset pairs. For instance, element 0, 3, and N-1 all share this pair. Also, I will take different action based on the timestamp value of each object with a given duration/offset pair. I will have built this array of objects in some other code, and could if I want restructure it. Instead of an array of objects, I was thinking of an object who's property names are the duration/offset pair, and their values are arrays which include id, type, etc objects. But a property name is only a single string so I thought maybe I could concatenate the two pairs into one. Seems like a hack, and it would been haven nice if the property name was identified by two strings (duration and offset).
  11. Thanks Jacques1, Wow, bet you didn't think I was capable of a non-hypothetical question Yes, a custom point can contain other custom points. Agree, array_keys() is better suited, and will do so. Getters, Setters, and now Loaders.... Sure, I like it!
  12. Thanks Requinix Why not a static variable? Everyone says the're evil for one. Secondly, does it make unit testing difficult? Would the method still be normal and not static? Agree the properties would definitely be public, but it still seems a bit shady. I see your point about a class with a single method being a little overboard. For the non-recursive idea, something like: while(!empty($stack)){ //do stuff which adds and removes from the stack until empty... }
  13. I was thinking of something like the following. Or maybe putting each in an array or object, and then just passing that back. private function _setPoints(array $items, $realPointIDs=[], $realPoints=[], $cacheReal=[], $aggrPoints=[], $aggrPointIDs=[], $cacheAggr=[], $custPointIDs=[], $custPoints=[]) { // bla bla bla $this->_setPoints($customNew, $realPointIDs, $realPoints, $cacheReal, $aggrPoints, $aggrPointIDs, $cacheAggr, $custPointIDs, $custPoints ); } }
  14. I have the following method. As you can see at the bottom, it calls itself and is recursive. Question is whether I should pass the current scope when I recursively call the method, or in the interim, store them in $this. I am assuming that I shouldn't use a static variable, right? Currently, I am storing the interim data in $this and adding to it every time the method is executed (for instance $this->realPoints[$point->id]=$point;), I have two concerns. First, it would make more sense to me if it returns the values instead of setting something which would later need to be requested (Reference http://forums.phpfreaks.com/topic/301316-should-methods-typically-return-multiple-objects/). Also, if the method is called twice outside itself, it gets doubled which is not my intent. Or should recursive functions explicitly have earlier data passed to them? For this case, I require realPointIDs, realPoints, cacheReal, aggrPoints, aggrPointIDs, cacheAggr, custPointIDs, and custPoints. Or should this single method have it's own class? I am thinking this might make the most sense. Any thoughts? Thank you private function _setPoints(array $items, $byName=false) { $model=$this->getModel(); $custPoints=[]; $points=$model->getPoints($items, false, $byName); foreach($points as $point){ if($point->type=="real" && !isset($this->realPoints[$point->id])){ $this->realPointIDs[]=$point->id; $this->realPoints[$point->id]=$point; if($this->cacheReal && $point->timestamp < $this->time-$this->cacheTimes['real_points']) { $this->cacheReal=false; } } if($point->type=="aggr" && !isset($this->aggrPoints[$point->id])){ $this->aggrPointIDs[]=$point->id; $this->aggrPoints[$point->id]=$point; if(!isset($this->cacheAggr[$point->duration.'~'.$point->offset]) && $point->timestamp < $this->time-$this->cacheTimes['aggr_points']) { $this->cacheAggr[$point->duration.'~'.$point->offset]=true; } } if($point->type=="cust" && !isset($this->custPoints[$point->id])){ $this->custPointIDs[]=$point->id; $custPoints[]=$point->id; } } if(!empty($custPoints)){ $customNew=[]; while($custPoint=$model->getCustomPoints($custPoints)){ //$this->custPointIDs[] already set $id=$custPoint->points_custom_id; unset($custPoint->points_custom_id); $this->custPoints[$id][]=$custPoint; $customNew[]=$custPoint->points_id; } $this->_setPoints($customNew); } }
  15. That is what I was implying under: catch (USERException $e) { //Deal with the error } Thank you all three for your replies. Okay, I concede using exceptions for validation isn't best. I heard the word "exceptional" behavior and situations a couple of times. I thought exceptions are used to deal with user behavior, and errors are used to deal with bugs in the script. What do you consider is exceptional?
  16. Consider the following. Also, please comment on my use of USERException. Thanks <?php try { $validate=new Validate(); $data=$validate->getValidatedData($_POST); //insert record } catch (USERException $e) { //Deal with the error } //New file class USERException extends Exception {} //Should this exception be defined here? Should another exception type be used? class Validate{ public function getValidatedData($data) { if(!isset($data['city'])){throw new USERException("City field is missing.");} if(!$this->isACity($data['city'])){throw new USERException("$data[city] is not a valid city.");} // ... return ['city'=>$data['city'],'etc'=>123]; } } ?>
  17. Thank you requinix, You point about whether a point mutates make sense. Thank you Jacques1,Your replies were helpful.
  18. I am just starting to think about this, haven't attempted to do anything with it, and unfortunately do not have any real examples. Okay, I see what you mean about deciding between a single getter and multiple getters depending on how the data is related, and there is no way anyone could answer without more details. Does a setter just set some property inside the object? Are PDO::prepare() and PDO::execute() setters, and PDO::fetch() a getter? If a setter is first executed, is it typically considered better practice to access the property directly, or through some method (which is a getter?) which then returns the property? Does a setter typically return the object (i.e. return $this;)?
  19. For instance, I can do this. $o=new MyObj(); $stuff=$o->getStuff(); $a=$stuff->a; $b=$stuff->b; $c=$stuff->c; Or should I do this? $o=new MyObj(); $o->setStuff(); $a=$o->a; $b=$o->b; $c=$o->c; Or maybe this? $o=new MyObj(); $a=$o->getA(); $b=$o->getB(); $c=$o->getC(); If one of the latter two, should setStuff() return $this so I can chain it? $a=$o->setStuff()->getA(); I recognize every situation is different, and am just looking for typical best practices. Thanks
  20. Is chaining methods considered good or bad practice? Please explain why or why not. If it is considered okay, please provide recommended formatting. Thanks return $db->prepare("SELECT a, b, c FROM mytable WHERE id=:id") ->execute(["id"=>123]). ->fetchAll(PDO:FETCH_OBJ);
  21. Thanks Jacques1, For now, I just wrote my own router. One of these days, I will start using a framework, and am sure one is built in, so why get married to another. I would expect the Apache rewrite engine is significantly faster and maybe will save a billionth of a second. Glad I am not the only one who thinks it is a PITA, and I will concede this small performance advantage for less pain.
  22. Ah, that is exactly what I want. Thanks! Do you have any recommendations? Have you tried any of the following? https://github.com/mrjgreen/phroute https://github.com/dannyvankooten/AltoRouter https://github.com/dannyvankooten/PHP-Router http://blogs.shephertz.com/2014/05/21/how-to-implement-url-routing-in-php/ http://www.kratedesign.com/blog/2010/03/php-router-and-clean-urls/ Yes, compared to the Apache rewrite engine.
  23. I was going to say something needs to be done in code. Maybe combining the composite key strings as a single unique property name. But then I read more on maxsd's link. ORM? Yea, I've heard of them, and they are on my list of things to learn up on..... If my question was posted before ORM's existed, would it make more sense?
  24. I recently tried http://www.slimframework.com/ to set up a REST API. Easy. Is there any reason I should use it or some other similar product for a normal website navigation? Previously, I put complicated (at least to me) regex in my httpd.conf (or .httacces) files to make URLs pretty. The redirects when using Slim is just to send everything to index.php if it isn't real. Yea, I am sure if volume is high, it isn't ideal, but what about when just starting off? Thanks
  25. Well if it doesn't make sense to you and Jacques, then it probably doesn't make sense to me either! Not really a search. Say I have $obj and $obj has properties "somePrimaryKey1", "somePrimaryKey2", and "somePrimaryKey3", I can access them by using $obj->somePrimaryKey1, etc. Turns out $obj wasn't created from a class, but was created from a database and PHP script. Maybe this is why it doesn't make sense. Is doing this unheard of?
×
×
  • 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.