Copying Objects : clone « Class « PHP






Copying Objects

 
<?
    class DogTag {
            public $Words;
    }

    class Dog {
            public $Name;
            public $DogTag;

            public function bark( ) {
                    print "Woof!\n";
            }

            public function _ _construct($DogName) {
                    print "Dog: $DogName\n";
                    $this->Name = $DogName;
                    $this->DogTag = new DogTag;
                    $this->DogTag->Words = "$DogName:555-1234";
            }
    }

    class Poodle extends Dog {
            public function bark( ) {
                    print "Yip!\n";
            }
    }

    $poppy = new Poodle("Poppy");
    print $poppy->DogTag->Words . "\n";
    
    function namechange($dog) {
            $dog->Name = 'Dozer';
    }

    namechange($poppy);
    print $poppy->Name . "\n";

    namechange(clone $poppy);
?>
  
  








Related examples in the same category

1.clone class instance
2.Change class instance after clone
3.clone a class
4.Cloning an aggregated class
5.Parents clone
6.Properly implementing cloning in aggregated classes