Core Function Clone
From Sputnik Wiki
(Difference between revisions)
(Created page with "<pre> Clone( <class> ) </pre> === Description === Create a clone of a class . === class === A class to clone. === Return Value === Success: New class with same data as the ...") |
(→Example) |
||
(5 intermediate revisions by one user not shown) | |||
Line 1: | Line 1: | ||
<pre> | <pre> | ||
− | Clone( < | + | Clone( <object> ) |
</pre> | </pre> | ||
=== Description === | === Description === | ||
− | Create a clone of a | + | Create a clone of a clone-able object. |
=== class === | === class === | ||
Line 13: | Line 13: | ||
=== Return Value === | === Return Value === | ||
− | Success: New | + | Success: New object with same data as the original |
Failure: Returns null | Failure: Returns null | ||
=== Remarks === | === 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. | If the class has __Clone() function then it will be called during the clone process to allow the class the ability to setup things. | ||
Line 24: | Line 26: | ||
=== Example === | === Example === | ||
+ | |||
+ | Example of cloning a binary variable | ||
+ | |||
+ | <syntaxhighlight lang="sputnik"> | ||
+ | $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' | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | Example of cloning a class | ||
<syntaxhighlight lang="sputnik"> | <syntaxhighlight lang="sputnik"> | ||
Line 54: | Line 74: | ||
// Clone the class $a and give it a new name and add something | // Clone the class $a and give it a new name and add something | ||
// to its $something array | // to its $something array | ||
− | $b = Clone $a; | + | $b = Clone($a); |
$b->$Name = "Barry"; | $b->$Name = "Barry"; | ||
$b->$Something[] = "FoX"; | $b->$Something[] = "FoX"; |
Latest revision as of 12:07, 21 June 2015
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 // } // }