Showing posts with label reference. Show all posts
Showing posts with label reference. Show all posts

Wednesday, 26 May 2021

Php: Object are assigned by reference

In Php, objects are always assigned by reference (It is not the same case with normal variables).

 

For example, Let’s create an Employee object.

$emp1 = new Employee(1, 'krishna');

 


Let’s assign $emp1 to $emp2.

$emp2 = $emp1;


 

As shown in the above image, both $emp1 and $emp2 points to the same memory location. Whatever the changes you are done via $emp2 will be reflected to $emp1 and vice versa.

 

Find the below working application.

 

assignment_by_reference_demo.php

#!/usr/bin/php

<?php

    class Employee{
        public $id;
        public $name;
        
        public function __construct($id, $name){
            echo "Inside constructor\n";
            $this->id = $id;
            $this->name = $name;
        }

        public function __toString() {
            return "id : $this->id\n". 
             "name: $this->name\n";
        }

        public function about_me($name){
            echo "\n\n$name details:\n";
            echo $this->__toString();
        }

    }

    $emp1 = new Employee(1, 'krishna');
    $emp2 = $emp1;

    $emp1->about_me('emp1');
    $emp2->about_me('emp2');

    echo "\nUpdating the name property using the reference emp2\n";

    $emp2->name='Shankar';
    $emp1->about_me('emp1');
    $emp2->about_me('emp2');


?>


Output

$./assignment_by_reference_demo.php 

Inside constructor


emp1 details:
id : 1
name: krishna


emp2 details:
id : 1
name: krishna

Updating the name property using the reference emp2


emp1 details:
id : 1
name: Shankar


emp2 details:
id : 1
name: Shankar









Previous                                                    Next                                                    Home

Monday, 26 April 2021

Php: Functions: pass the argument by reference

If you pass an argument to the function by reference, any changes made to the variable inside the function are visible outside also.

 

Example

function square(&$a){

    $a = $a * $a;

}

 

In the above snippet, square function expecting a variable reference(same is conveyed to php using &).

 

function_pass_by_reference.php

#!/usr/bin/php

<?php
    function square(&$a){
        $a = $a * $a;
    }

    $ele = 10;

    echo "Value of ele : $ele\n";
    echo "Calling square function\n";

    square($ele);
    echo "Value of ele : $ele\n";
?>

 

Output

$./function_pass_by_reference.php 

Value of ele : 10
Calling square function
Value of ele : 100

 

 

 

  

Previous                                                    Next                                                    Home

Monday, 27 April 2020

Javassist: Add non-primitive fields to a class

Follow below step by step procedure to add non-primitive field to a class.

Step 1: Define Class Point.
public class Point {
    .....
    .....
}

Step 2: Get an instance of ClassPool.
ClassPool classPool = ClassPool.getDefault();

Step 3: Get CtClass instance.
CtClass ctClass = classPool.makeClass("com.sample.app.model.Demo");

Step 4: Get CtClass instance for Point class.
CtClass pointClass = classPool.get("com.sample.app.model.Point");

Step 5: Define CtField using pointClass.
CtField ctField = new CtField(pointClass, "point", ctClass);

Step 6: Add the field to CtClass.
ctClass.addField(ctField);

Find the below working application.

Point.java
package com.sample.app.model;

public class Point {
    private int x;
    private int y;

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

}

App.java
package com.sample.app;

import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;

public class App {

    public static void main(String args[]) throws Exception {
        ClassPool classPool = ClassPool.getDefault();

        CtClass ctClass = classPool.makeClass("com.sample.app.model.Demo");

        CtClass pointClass = classPool.get("com.sample.app.model.Point");
        CtField ctField = new CtField(pointClass, "point", ctClass);

        ctClass.addField(ctField);
        
        ctClass.writeFile("/Users/Shared/javassist");
    }
}

Run App.java.

You can observe Demo.class file is created at "/Users/Shared/javassist".
$tree /Users/Shared/javassist
/Users/Shared/javassist
└── com
    └── sample
        └── app
            └── model
                └── Demo.class

4 directories, 1 file

Open Demo.class file in java decompiler to see the source code.


Previous                                                    Next                                                    Home