Skip to content

Instantly share code, notes, and snippets.

@minte9
minte9 / index.php
Last active April 14, 2021 07:24
Php / Basics / Reference
<?php
/**
* array_map()
*
* Round up array values
* Use array_map() with built-in ceil functions
* The result array $mapped will be [2, 3, 4]
*/
<?php
/**
* Given the users $role and access $rights ...
* Check if Mike has admin rights and downgrade him
* Wrong approach:
* if ($is_admin && $can_edit_articles ...
*/
$rights = (object) ["read"=>1, "write"=>2, "readwrite"=>16, "admin"=> 32];
$role = (object) ["jim"=>96, "mike"=>32];
/**
* Define Dog class using supertype Animal
* Use safeguard annotation.
*/
class App {
public static void main(String[] args) {
Dog d = new Dog();
d.test(); // Dog
}
/**
Define MyMath.floor() method using java.lang.math method
class App {
public static void main(String[] args) {
double n = MyMath.floor(12.3);
System.out.print(n); // 12.0
}
}
*/
/**
* Define abstact class Animal
* Class Animal contains two methods, eat() and showName()
* One of the method is abstract
*/
class App {
public static void main(String[] args) {
Dog d = new Dog();
d.setName("MyDog");
System.out.println(d.name); // MyDog
/**
* Define Dog class which
* Dog class must implement the correct class
*/
class App {
public static void main(String[] args) {
Dog d = new Dog();
d.bark(); // The dog is barking
}
}
/**
* Write constructor and method for Output class.
* The output() prints the <name> instance variable
*/
class App {
public static void main(String[] args) {
Output o = new Output("John");
o.output(); // John
}
}
<?php
/**
* Define class constructor and ...
* Display the constructor method name
*/
$obj = new A();
class A
{
/**
* Import libraries required by the following code
*/
/**
* SOLUTION
*/
import java.util.List;
import java.util.ArrayList;
/**
* This code will throw an Exception Error
* Correct the error line
*/
class App {
public static void main(String[] args) {
/**
* SOLUTION