Jump to content

NotionCommotion

Members
  • Posts

    2,446
  • Joined

  • Last visited

  • Days Won

    10

Everything posted by NotionCommotion

  1. Thank you all! Given more thought, I totally agree that I shouldn't use a constructor to do something. If the class only has one purpose, why make it happen using a constructor, and instead I should just use a simple function. Agree? Jacques1 suggestion of using the observer pattern has merit, and I will probably do so. Still curious why you all feel anonymous callback functions are such a bad idea? The whole purpose of this script is to respond to a jQuery plugin, and typically JavaScript/jQuery often uses anonymous callback functions for fairly sophisticated requirements. When configuring the server script, it would maybe be nice to follow the same design pattern as when configuring the jQuery plugin.
  2. Thanks Scootstah, My original question dealt with with how to keep the base class intact and add additional script to update the database, and not how to implement a router/controller. Maybe, however, I need help on that part as well. You stated earlier: Also, for the record, constructors should only be concerned with setting up the class... and not actually doing anything. Originally I was planning on putting the controller just for tasks associated with this particular class in the constructor and implementing the appropriate task based on a GET or POST value. Why shouldn't the constructor do something other than set up a class?
  3. Thanks Jacques1, Guess that makes sense, but will need to give it a bit more thought. Why do you think doing so is more appropriate than something like the following: new myImageCropper($arr); class myImageCropper extends imageCropper { public function update() { parent::update(); //SQL to update database goes here } }
  4. I never heard of observer pattern before and am reading up on it. How do you envision it being used? Thanks
  5. Hello Scootstah and Requinix, I replied to Jacques1 give some context on what I am attempting to accomplish. Does your advise change? Thanks
  6. Hi Jacques1. Long time no see. Hope all is good. Maybe no problems with the code. At my day job now and wrote it without trying it. I've never tried to do something like this, and didn't know I was close. What am I trying to do? I wrote a little jQuery plugin which allows the user to select an image file on their local PC and then select the area of the image which they want, and the file and desired area coordinates are sent to the server. It also allows the user to delete the previously uploaded image and replace the target with a default image. The JavaScript is similar to: $('.target').imageCropper('/path/to/server/script.php?task=doSomething'); To accompany the plugin, I also wrote a PHP class which has four public methods, and the method to execute is based on $_GET or $_POST['method_to_execute']. init(). The configuration of the plugin is all defined serverside and the plugin gets the configuration settings from the server when it is initialed. upload(). Uploads the image and saves it in a temporary folder. update(). Crops and resizes the image and moves it to a permanent folder. delete(). Deletes the previously image which is stored in the permanent folder. For the update() and delete() methods, I wish to allow some other custom functionality such as updating the database. The PHP configuration would be something similar to the following: public function doSomething() { $arr=array( 'path_to_save'=>'bla/bla/bla', 'ratio'=>2, 'default_image'=>'/bla/default.png', 'allowed_extentions'=>array('png','jpg'), 'other_stuff_to_override_default_settings'=>'bla', 'save'=> function($col1,$col2) { // call back to save filename in database $stmt=$this->pdo->prepare('UPDATE mytable SET col1=?, col2=? WHERE id=?'); $stmt->execute($col1,$col2,$this->id); }, 'delete'=>function() { //As required } ); new imageCropper($arr); //Based on $_GET or $_POST['method_to_execute'], execute the appropriate method. } Make sense?
  7. How do I pass a callback function to a constructor, and be able to use variables from both the parent class and new class? The sample code below will hopefully make my question more clear. Thank you <?php $controller=new controller(); $controller->doSomething(); class controller { public $pdo; public $id=333; public function __construct() { $this->pdo= new PDO ('mysql:dbname=testdb;host=127.0.0.1', 'dbuser', 'dbpass'); } public function doSomething() { $arr=array( 'a'=>123, 'b'=>321, 'save'=> function($col1,$col2) { $stmt=$this->pdo->prepare('UPDATE mytable SET col1=?, col2=? WHERE id=?'); $stmt->execute($col1,$col2,$this->id); } ); new myClass($arr); } } class myClass { public function __construct($options) { // Do some stuff... $col1=111; // Assume $_POST['col2_value'] is equal to 222 $options['save']($col1, $_POST['col2_value']); //Execute 'UPDATE mytable SET col1=111, col2=222 WHERE id=333' } }
  8. I would expect BINARY would be more efficient, however, wouldn't it need to be converted using either PHP's hex2bin() or MySQL's UNHEX()? <?php $password='rasmuslerdorf'; echo($password."<br>"); $hash=password_hash($password, PASSWORD_BCRYPT, ['cost' => 12]); echo($hash."<br>"); $binary_hash=hex2bin($hash); echo($binary_hash."<br>"); ?> rasmuslerdorf $2y$12$GkCH8rRmTfHuBFG/WEdPbuULI29p7vKCHlt2o7w3dkvtgeXlFsLja Warning: hex2bin(): Input string must be hexadecimal string in /var/www/main/html/testing/hashing2.php on line 6
  9. Hi Hansford and QuickOldCar, I ended up going away from using PASSWORD_DEFAULT, and will explicitly use PASSWORD_BCRYPT. As such, it will always be 60 characters. If I later elect to change algorithms, I will change the database schema at that time. That being the case, I would expect I would want to use CHAR(60) or maybe some sort of BINARY.
  10. http://php.net/manual/en/function.password-hash.php shows a simple example to hash a password using BCRYPT. I've read different posts recommending CHAR(60), BINARY(60), BINARY, and even BINARY(40). What are the pros and cons of using one datatype over another? <?php /** * In this case, we want to increase the default cost for BCRYPT to 12. * Note that we also switched to BCRYPT, which will always be 60 characters. */ $options = [ 'cost' => 12, ]; echo password_hash("rasmuslerdorf", PASSWORD_BCRYPT, $options)."\n"; ?>
  11. Don't think you need binary trees, just simple math. What are you summing over? Probably could just do in in SQL, however, I suppose if you want, you could iterate over an array.
×
×
  • 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.