Quick explanation of PHP property types

If you have little object-oriented programming experience in PHP you might be a little confused by all the new terms in OOP programming compared to regular functional programming. Before we begin, remember the difference between the terms initialize and instantiate. This is common for all programming languages, not just PHP.

Initialize = assign a variable to a value (all variables/properties must have an initial value when created, even if the compiler does this automatically)
Instantiate = create a new instance of an object (which could be assigned to a variable).

In other words, you could initialize a variable and assign it to a class instantiation. Now, take a look at the example class below with some methods to describe how they work in practice:

<?php
/* CLASS */
class ITDB
{
private function __construct()
{
// Constructor runs when class is instantiated,
// like this: $test = new ITDB();
}

private function hello1()
{
// Only accessible in current class
}
protected function hello2()
{
// Only accessible in current class AND
// child classes which inherits this one
}
public function hello3()
{
// Accessible to all classes
}

// By declaring the method as "static" we can call the
// methods without having to instantiate the class first
static public function hello4()
{
// Accessible without having to instantiate a
// new ITDB() instance first
}
}

/* CLIENT */
ITDB::hello1(); // Will not work because method is private

$sayhi = new ITDB(); // Instantiate class, assign to $sayhi
$sayhi->hello1(); // Will not work, hello1() is still private
$sayhi->hello3() // This is valid, hello3() is a public method

ITDB::hello4(); // Also valid, hello4() is a public static method

Let me know in the comments if you have any questions regarding this 🙂

Composer failed to install Laravel because of missing zip extension

I was trying to install Laravel by running the command composer global require laravel/installer but it failed because laravel/installer v2.0.x requires ext-zip.

Turns out it wasn’t installed on my system. To fix it, first find out your php version by running php -v. Then use the php version number to install the missing extension like php7.2-zip

sudo apt install php7.2-zip

This will also work for other extensions like php7.2-mbstring and php7.2-dom

How to retrieve random posts from WordPress

Revised:

 

 

 


Old:

This is just a quick solution I cooked up. Performance-wise it won’t be the best because it’s actually retrieving all posts, then shuffling the array and finally pick out 3 of them.

Here it is anyways:

function ShowRandomPosts() { 
    $get_random_posts = get_posts(); 
    shuffle($get_random_posts); 
    echo '<div style="padding:10px;">'; 
    echo '<h3>Random posts</h3>'; 
    for($i=0; $i<3; $i++) { 
        echo '<div>'; 
        echo '<a href="'.get_post_permalink($get_random_posts[$i]->ID).'" style="color:#fff;">'; 
        echo '<img src="'.get_the_post_thumbnail_url($get_random_posts[$i]->ID).'" style="max-width:100px;"><br>'; 
        echo '<h4>'.$get_random_posts[$i]->post_title.'</h4>'; 
        echo '</a>'; 
        echo '</div>'; 
    } 
    echo '</div>';
}

Note that the HTML and inline CSS is just an illustration so you would probably want to change that.