Core Function Clone

From Sputnik Wiki
Jump to: navigation, search
Clone( <object> )

Contents

Description

Create a clone of a clone-able object.

class

A class to clone.

Return Value

Success: New object with same data as the original

Failure: Returns null

Remarks

When used on a class:

If the class has __Clone() function then it will be called during the clone process to allow the class the ability to setup things.

Its worth noting __Clone() does not get called until all the entire clone process is complete

Example

Example of cloning a binary variable

$a = BinaryCreate(3);
$a[0] = 'C';
$a[1] = 'A';
$a[2] = 'T';
 
$b = Clone($a);
$b[0] = 'J';
 
echo "First '$a' Second '$b'\n";
# Prints
# First 'CAT' Second 'JAT'

Example of cloning a class

// Define a new class
Class Test
{
	Static $Instances = 0;
	my $ID;
	my $Name;
	my $Something;
	// The Construct will let us know when it happens
	Function __Construct($Name)
	{
		say "Creating a new class with name: $Name...";
		$this->$ID = ++Test::$Instances;
	}
	// The Clone will let us know when it happens
	Function __Clone()
	{
		say "Cloning a class with name: $Name...";
		$this->$ID = ++Test::$Instances;
	}
};
 
// Create a new instance of the class and give it a name etc
$a = new Test("John");
$a->$Name = "John";
$a->$Something = array("Cat", "Dog", "Frog");
 
// Clone the class $a and give it a new name and add something
// to its $something array
$b = Clone($a);
$b->$Name = "Barry";
$b->$Something[] = "FoX";
 
// Print the contents of the classes
printr $a;
printr $b;
 
// Prints:
// Creating a new class with name: John...
// Cloning a class with name: John...
// {CLASS:test;ID:3}
// {
//         [id] => 1
//         [name] => John
//         [something] => ARRAY
//         {
//                 [0] => Cat
//                 [1] => Dog
//                 [2] => Frog
//         }
// }
// {CLASS:test;ID:4}
// {
//         [id] => 2
//         [name] => Barry
//         [something] => ARRAY
//         {
//                 [0] => Cat
//                 [1] => Dog
//                 [2] => Frog
//                 [3] => FoX
//         }
// }
Personal tools
Namespaces
Variants
Actions
Navigation
Toolbox