N
N
Normant2016-03-15 00:00:48
OOP
Normant, 2016-03-15 00:00:48

How to clone an object with passed properties in PHP?

Friends, such a question. There is a class that allows you to resize images (class.upload.php), add filters, etc.
There was a need to change images of different sizes in a similar way.
I find the solution that I implemented idiotic, because the code is repeated and right there:

$foo = new Upload($forResize);
$boo = new Upload($forResize);

$foo->file_new_name_body	= $imagename;
$foo->image_resize			= true;
    
if($_POST['bg'] == 1)
{
  $foo->image_ratio					= true;
  $foo->image_ratio_fill			= true;
  $foo->image_background_color= '#FFFFFF';
      
  $boo->image_ratio					= true;
  $boo->image_ratio_fill			= true;
  $boo->image_background_color= '#FFFFFF';
}
else
{
  if($_POST['crop'] == "l" or $_POST['crop'] == "L" or $_POST['crop'] == "л" or $_POST['crop'] == "Л")
  {
    $foo->image_ratio_crop      = 'L';
    
    $boo->image_ratio_crop      = 'L';
  }
  elseif($_POST['crop'] == "r" or $_POST['crop'] == "R" or $_POST['crop'] == "п" or $_POST['crop'] == "П")
  {
    $foo->image_ratio_crop      = 'R';
    
    $boo->image_ratio_crop      = 'R';
  }
  else
  {
    $foo->image_ratio_crop      = true;
      
    $boo->image_ratio_crop      = true;
  }
}
    
$foo->image_y				= 270;
$foo->image_x				= 270;	
$foo->Process($uploaddir."middle/");
$foo->processed;
    
$boo->file_new_name_body	= $imagename;
$boo->image_resize			= true;
$boo->image_y				= 78;
$boo->image_x				= 78;
$boo->Process($uploaddir."small/");
$boo->processed;

Thought to do something like:
$foo = new Upload($forResize);
$foo->file_new_name_body	= $imagename;
$foo->image_resize			= true;
    
if($_POST['bg'] == 1)
{
  $foo->image_ratio					= true;
  $foo->image_ratio_fill			= true;
  $foo->image_background_color= '#FFFFFF';	
}
else
{
  if($_POST['crop'] == "l" or $_POST['crop'] == "L" or $_POST['crop'] == "л" or $_POST['crop'] == "Л")
  {
    $foo->image_ratio_crop      = 'L';
  }
  elseif($_POST['crop'] == "r" or $_POST['crop'] == "R" or $_POST['crop'] == "п" or $_POST['crop'] == "П")
  {
    $foo->image_ratio_crop      = 'R';
  }
  else
  {
    $foo->image_ratio_crop      = true;
  }
}
$foo->image_y				= 270;
$foo->image_x				= 270;	
$foo->Process($uploaddir."middle/");
$foo->processed;
    
$boo = clone $foo;
$boo->image_y				= 78;
$boo->image_x				= 78;
$boo->Process($uploaddir."small/");
$boo->processed;

But this option does not work properly, the properties passed to the object above cloning are not found in the clone.
How to clone an object with passed properties in PHP?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question