Thursday, 13 May 2021

Php: Class inheritance

Inheritance is a feature where one class reuse the methods, properties defined in other class.

 

How can I inherit the properties and behaviours from a class?

Using extends keyword, you can specify from which class you want to inherit the properties, methods.

 

Syntax

class Parent{
  
}

class Child extends Parent{
  
}

In the above example, Child class inherit the properties, methods from Parent class. Any object of child class can use the properties, methods defined in Parent class. Let’s see the same with below example.

 

inheritance_demo.php

#!/usr/bin/php

<?php

   class Employee{
        var $id;
        var $first_name;
        var $last_name;
        var $country='India';

        function about_me(){
            echo "Hello ($this->first_name,$this->last_name), you are from $this->country\n";
        }
   }

   class Manager extends Employee{
       var $reportee_ids;
   }

   $manager1 = new Manager();
   $manager1->id = 1;
   $manager1->first_name = 'Vadiraj';
   $manager1->last_name = 'Arora';
   $manager1->reportee_ids = array(2, 3, 43, 21);

   $manager1->about_me();

   echo $info;
?>

 

Output

$./inheritance_demo.php 

Hello (Vadiraj,Arora), you are from India

 

 



 

 

Previous                                                    Next                                                    Home

Php: Convert an object to string

By implementing __toStirng() method in your class definition, you can specify how your object should be represented in string format.

 

If your class implements __toSting() method, you can conver object to string representation via type casting.

 

Example

$info = (string)$emp1;

 

to_string_demo.php

#!/usr/bin/php

<?php

   class Employee{
        var $id;
        var $first_name;
        var $last_name;
        var $country='India';

        function __toString(){
            return "Hello $this->first_name, you are from $this->country\n";
        }
   }

   $emp1 = new Employee;
   $emp1 -> id = 1;
   $emp1 -> first_name = 'Krishna';
   $emp1 -> last_name = 'Gurram';
   
   $info = (string)$emp1;
   echo $info;
?>

 

Output

$./to_string_demo.php 

Hello Krishna, you are from India

 

 

 

  

Previous                                                    Next                                                    Home

Jq: Get all the firstnames of employees from json document

 

compOperators.json 

{
	"1": {
		"org": [{
			"name": "ABC Cord",
			"yrsOfExperience": 2.2
		}, {
			"name": "XYZ",
			"yrsOfExperience": 1.8
		}],
		"firstName": "Krishna",
		"lastName": "Ram",
		"address": {
			"city": "Bangalore",
			"country": "India"
		}
	},

	"2": {
		"org": [{
			"name": "XYZ",
			"yrsOfExperience": 4.2
		}, {
			"name": "Bokara",
			"yrsOfExperience": 3.8
		}],
		"firstName": "Battu",
		"lastName": "Gopi",
		"address": {
			"city": "Bangalore",
			"country": "India"
		}
	},

	"3": {
		"org": [{
			"name": "Dongle",
			"yrsOfExperience": 2.6
		}, {
			"name": "Binare",
			"yrsOfExperience": 1.11
		}],
		"firstName": "Krishna",
		"lastName": "Ram",
		"address": {
			"city": "Hyderabad",
			"country": "India"
		}
	}

}

 

Get all the employees firstNames

 

$cat compOperators.json | jq '.[].firstName'
"Krishna"
"Battu"
"Krishna"


Get all the employees firstName and lastName

$cat compOperators.json | jq '.[] | .firstName + "," + .lastName'
"Krishna,Ram"
"Battu,Gopi"
"Krishna,Ram"



Previous                                                    Next                                                    Home

jq: functions

jq command line tool several functions that are used while processing json data.

 

Example

keys: Return all the keys of json document

length: Return length. Output of ‘length’ function depends on the input.

a.   If a string is passed, then it returns the number of characters in the string

b.   If an array is passed, then it return the number of elements in the array

c.    If an object is passed, then it return the number of key-value pairs.

 

empPretty.json 

{
	"org": [{
		"name": "Honeywell",
		"yrsOfExperience": 2.2
	}, {
		"name": "IBM",
		"yrsOfExperience": 1.8
	}],
	"firstName": "Krishna",
	"lastName": "Ram",
	"address": {
		"city": "Bangalore",
		"country": "India"
	}
}

 

Get all the keys of json document

$cat empPretty.json | jq 'keys'
[
  "address",
  "firstName",
  "lastName",
  "org"
]

 

Get all the key of ‘org’ property

$cat empPretty.json | jq '.org[0] | keys'
[
  "name",
  "yrsOfExperience"
]

 

Get number of elements in the org array

$cat empPretty.json | jq '.org | length'
2

Get length of city property

$cat empPretty.json | jq '.address.city | length'
9
$
$cat empPretty.json | jq '.address | .city | length'
9



 

 

 

 

 

 

  

Previous                                                    Next                                                    Home

Jq: pipe : combine two filters

‘jq’ command support Unix | kind of syntax to combine two filters.

 

empPretty.json 

{
	"org": [{
		"name": "Honeywell",
		"yrsOfExperience": 2.2
	}, {
		"name": "IBM",
		"yrsOfExperience": 1.8
	}],
	"firstName": "Krishna",
	"lastName": "Ram",
	"address": {
		"city": "Bangalore",
		"country": "India"
	}
}

 

For example, '.address.city' is same as '.address | .city'

$cat empPretty.json | jq '.address | .city'
"Bangalore"
$
$cat empPretty.json | jq '.address.city'
"Bangalore"

 

 

 

 

Previous                                                    Next                                                    Home

Sunday, 9 May 2021

Php: $this: refer to current instance properties

When you are inside a class definition, you should use the special variable $this, to refer current instance properties or methods.

 

Example

$this->first_name;

$this->print_full_name();

 

Find the below working application.

 

this_variable_demo.php

#!/usr/bin/php

<?php

   class Employee{
        var $id;
        var $first_name;
        var $last_name;
        var $country='India';

        function print_full_name(){
            $full_name = $this->first_name . ',' .$this->last_name;
            echo "full_name: $full_name";
        }
        function about_me(){
            echo "id : $this->id\n";
            $this->print_full_name();
            echo "\ncountry : $this->country\n";
        }
   }

   $emp1 = new Employee;
   $emp1 -> id = 1;
   $emp1 -> first_name = 'Krishna';
   $emp1 -> last_name = 'Gurram';
   
   $emp1 -> about_me();
?>

 

Output

$./this_variable_demo.php 

id : 1
full_name: Krishna,Gurram
country : India

 

 

 

 

Previous                                                    Next                                                    Home

Jq: Processing wget command output

Just like how you are piping json data from echo command, from a file, you can even extract the ‘wget’ command output using jq tool.

 

Example 1: Get name of github project.

$wget -cq https://api.github.com/repos/harikrishna553/springboot -O - | jq '.name'
"springboot"

 

Example 2: get url of github project.

$wget -cq https://api.github.com/repos/harikrishna553/springboot -O - | jq '.html_url'
"https://github.com/harikrishna553/springboot"

 

Example 3: Get name, url, size and full name of the github project.

$wget -cq https://api.github.com/repos/harikrishna553/springboot -O - | jq '.name, .html_url, .size, .full_name'
"springboot"
"https://github.com/harikrishna553/springboot"
520
"harikrishna553/springboot"

 

How to install wget?

Step 1: Install brew

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

 

Step 2: Install wget

brew install wget

 

 

 

 

Previous                                                    Next                                                    Home