NotionCommotion
Members-
Posts
2,446 -
Joined
-
Last visited
-
Days Won
10
Everything posted by NotionCommotion
-
Extending classes and methods question
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
I am still trying to understand how this would work. Are you saying that the child implements the parent abstract method, and that parent method calls a method defined in the child to get the necessary value? -
Extending classes and methods question
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
@Kicken. I just went over your actual script examples. For your first example, seems like this is what you are doing. No? Instead of calling the method "doSomething()", you call it "doSomethingInternal()". From a naming prospective, anything wrong with calling the internal method "_doSomething()"? You did create an abstract method "doSomething()", but it is empty. What is the point? To just let people know it is being defined in the child, or is there something more? For your second example, I see how you are using getExtra(). Where does the success() and failure() methods come in? Thanks @Requinx. Do you mind giving me a brief script example how this would be implemented? Something similar to my original and the one that Kicken provided so I could compare? Thanks -
Extending classes and methods question
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
Thanks Kicken, Yea, I see your point and don't disagree with your reasons why it is bad. I've totally ignored (or at least 96% ignored as I have in fact created some abstract classes without really knowing why) the concepts of abstract, traits, and the like, but expect I need to do differently. If you know of any good resources to learn up, please advise. Regardless, still appreciate your help. -
Extending classes and methods question
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
Exactly! Basically what I am doing, but using the same name. Why not use the same name and do my default argument value hack? If not, is there an accepted naming standard? For instance, if child method is "doThis()", parent helper method should be "_doThis()".. I really need to get better at this! Are you willing to give me some pseudo-real examples on how this works? Or point me to some literature which I could read through, and hopefully answer questions regarding it? Thanks you! -
Extending classes and methods question
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
Thanks Requinx, The child class will never be passed an argument, and the parent will always be passed one.. The only reason I included the "dummy" argument is so I could use the same method name in each without generating a warning/error. Does this change your recommendations? Will do. I was thinking of passing a very simple anonymous function, but was chastised a while back... -
I have a method in several child classes which are basically the same, but have some small differences, and I would like to have most of the script in the parent class. The following would basically work, but I would rather execute $this->bla_a() before echoing anything. How would you recommend doing so? Thanks $controller=new ChildController1(); $controller->doSomething(); class ParentController { protected function doSomething($extra) { header('Content-Type: application/json;'); $id=(isset($_POST['id']))?$_POST['id']:0; $other=new otherclass(); $x=$other->bla($id,$extra); $flag=$x['flag']; unset($x['flag']); echo(json_encode($x)); return $flag; } } class ChildController1 extends ParentController { public function doSomething($extra='dummy') { if(parent::doSomething('usethis1')){ $this->bla_a(); } } } class ChildController2 extends ParentController { public function doSomething($extra='dummy') { if(parent::doSomething('usethis2')){ $this->bla_b(); } } }
-
Strategies to uploading files to the server
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
Hi Jacques, I am concerned that showing all the authorization rules will divert focus. Guess if I better organized them, might not be the case, but unfortunately it is the case. Yes, I have considered using transactions and will do so if they provide value. Don't think it is necessary on the option 2. I didn't say so, but the authorization rules will require a database query. Seems that this is also a potential reason to go with option 2. While I framed the question as I did, part (or most) of my dilemma is how to scope functions of a class to perform most of this scope. I want a class which is responsible to upload and validate the file, store the file in the documents table, move the file to its correct directory, and even add these link junction records. At least, I think this is what I want, but I have been going back and forth. I believe the audit trails which I previously mentioned are outside of what this class should do, and plan on doing them after the action occurs. Then comes these authorization rules that I mentioned. They seem like they should be defined outside of this class which makes me question my option 2, but maybe they can still be defined outside and implemented within the class? Hope I am not overly confusing the issue. -
I have an application where users can create one of 10 types of entity records, and 8 of these record types are identified by a single primary key, and 2 of them are identified by a duel composite key. Users are then able to upload files to the server provided the file validates physically (i.e. file size, extension, etc), and whether the file can be "linked" to one of the previously mentioned entity records based on various "authorization" rules which will be promoted by the JavaScript client and enforced by the server either using PHP code and/or the database. Upon successfully uploading a file and linking it to an entity record, several other transactions occur such as audit trails, etc. My plan is to create the 10 described entity records table, a single "documents" table, and 10 junction tables to save the link state to the 10 entity tables. For this discussion, please assume this schema is correct. I would like recommendations on the steps to implement. One option is when a file is submitted to the server... Verify that all necessary POST/FILE data is provided Verify that the file passes physical validation. Make sure the authorization rules are met. Store the file statistics in the "documents" table and retrieve its primary key. Create the junction link record. Move the file to the correct location. Save audit trails, etc. Another option is when a file is submitted to the server... Verify that all necessary POST/FILE data is provided Verify that the file meets passes physical validation. Store the file statistics in the "documents" table and retrieve its primary key. Attempt to create the junction link record using a query than must pass authorization rules. If inserted... Move the file to the correct location. Save audit trails, etc. If not inserted... Delete the file statistics in the database Recommendations on which approach, or whether it should be a different approach all together?
-
Hi Ginerjm, bla() was just an example. Instead of a function, assume it was a bunch of script which I didn't wish to duplicate in getstuff(). Did you look into []? Just a shortcut for array(), and [1,2,3] is the same as array(1,2,3).
-
Try the following. Also, experiment with using $key=>$value in your foreach loops. <?php $array=[ 'catalog'=>[ [ 'attributes'=>['book'=>20160122], 'section'=>[ ['id'=>'F100','title'=>'Across the Sea'], ['id'=>'F101','title'=>'Blue Water'], ['id'=>'F102','title'=>'Red Rove'] ] ], [ 'attributes'=>['book'=>20160123], 'section'=>[ ['id'=>'F103','title'=>'xAcross the Sea'], ['id'=>'F104','title'=>'xBlue Water'], ['id'=>'F105','title'=>'xRed Rove'] ] ], ]]; foreach($array['catalog'] as $catalog){ echo('Book: '.$catalog['attributes']['book']."\n"); foreach($catalog['section'] as $section){ echo($section['id'].' '.$section['title']."\n"); } echo("\n"); } ?>
-
Thanks Jacques, If I wasn't already convinced, I definitely am now.
-
"bla" does something, and is not relevant to the question. "[]" is an array (you might want to look into it). I believe the functions are consistent. Thank you. This is very relevant. So, you recommend the first as it is more universal. Good point. The second, however, reduces content of code assuming the function is used often. Note sure if it is worth it, and thus why I am asking the question.
-
Please comment on the pros and cons of the two approaches. Thank you <?php $stuff1=isset($_POST['stuff'])?getStuff1($_POST['stuff']):[]; function getStuff1($value){ return bla($value); } $stuff2=getStuff2('stuff'); function getStuff2($name){ return isset($_POST[$name])?bla($_POST[$name]):[]; } ?>
-
A session? Evil localhost, got to worry about that one
-
This is impossible! There is no way this is happening, but it is! It defies gravity! Whenever I've thought I witnessed the impossible, I've always eventually stumbled upon some tiny detail which makes what I've witnessed exactly as it should be.
-
Is this your only form? Are you sure your JavaScript is creating inputs? Do they have names? Consider adding a name to your submit input so you could check that. <form method = 'POST'> <?php echo $xml->from ?>: <script>DateInput('ordersDateFrom', true, 'YYYY-MM-DD')</script> <?php echo $xml->to ?>: <script>DateInput('ordersDateTo', true, 'YYYY-MM-DD')</script> <input type = 'submit' value = '<?php echo $xml->submit; ?>'> </form> I saw that your print_r you added per Psycho's advice was nested in a table. Have you checked the HTML to make sure it wasn't just not being displayed. Have you tried syslog to make sure you are not hitting the server multiple times (or not at all)? Not your question, but I would recommend doing your PHP logic first and then your presentation. While a template engine is not needed (and some will say not to use one at all and just use PHP), I like them and recommend Twig.
-
Try using syslog. Maybe something like syslog(LOG_INFO,print_r($_POST,1)); at the top of the page (after any setup of syslog if you do so in your script, of course). Also, use syslog to log applicable $_SERVER parameters.
-
Encoding using Crockford's Base32
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
Yes I was. On the way to work, thought about it another way. 2^128=32^x, then x=128*log(2)/log(32)=25.6. Been a while since I've done this! Now I see 32=2^5, so really the same as your example. Not a PHP question but a UX question..... Would you rather type in 26 alphanumerical values or 39 numbers? -
Encoding using Crockford's Base32
NotionCommotion replied to NotionCommotion's topic in PHP Coding Help
Thanks Jacques, Seems to work fine. According to http://www.crockford.com/wrmg/base32.html, it is used for numbers, and the earlier class seemed to only work with numbers as well. The new number of characters always appears to be 26. Why is that? Thanks -
Using class https://github.com/dflydev/dflydev-base32-crockford, I am trying to encode a random number. Below is my failed attempt. Trying to typecast the $decimal into an integer also sets it to zero. How is this best accomplished? Thanks $binary=mcrypt_create_iv(16, MCRYPT_DEV_URANDOM); echo($binary."\n"); $hex=bin2hex($binary); echo($hex."\n"); $decimal=hexdec($hex); echo($decimal."\n"); $encodedValue = Crockford::encode($decimal); echo($encodedValue."\n"); 4?1V*8G?ۑL?? 34fe31562ac28b3847f0db914cfe19cf 7.043969984781E+37 0
-
Works for me. Thanks
-
Where do they make sense? Being unpredictable by itself can't be the reason. They don't appear to be more readable than other solutions, and maybe less. Agree? I assume the primary reason is that they require less server resources. Is this the only compelling reason?
-
Thanks Jacques1, Any reason not always to use a 128-bit random number and encode it for readability? Is there a class to implement Crockford's solution, or need I code it myself? By the way, never knew why "U" was excluded before reading it. No, still with PHP5. Guess it is time to upgrade! Thanks again
-
Help sanitizing this script against mysql injection
NotionCommotion replied to ababba2's topic in PHP Coding Help
Sorry, maybe not the answer you were looking for, but ajax has nothing to do with the database. You are sending a request to the PHP server, the server validates and saves the data, and replies with typically HTML if a standard request, or JSON or similar if an ajax request.