Richard Castera - Explorations in Software Development
Richard Castera - Explorations in Software Development
Browsing Tag
php
Resources Scripts

PHP – Convert Array to Object with stdClass

July 5, 2009 53 Comments

The PHP stdClass() is something that isn’t well documented but i will try to shed some light into the matter. stdClass is a default PHP object which has no predefined members. The name stdClass is used internally by Zend and is reserved. So that means that you cannot define a class named stdClass in your PHP code.

It can be used to manually instantiate generic objects which you can then set member variables for, this is useful for passing objects to other functions or methods which expect to take an object as an argument. An even more likely usage is casting an array to an object which takes each value in the array and adds it as a member variable with the name based on the key in the array.

Here’s an example below that converts an array to an object below. This method is called Type Casting.

Continue reading
Reading time: 1 min
Share:
Written by: rcastera
Resources Scripts

PHP – getting a list of currently loaded classes

June 25, 2009 1 Comment

In some cases you may need to investigate which classes are loaded in the current scope. You can do it pretty fine with the get_declared_classes() function. This function will return an array of currently available classes.

PHP
1
2
3
4
<?php
    include_once("class.myClass.php");
    print_r(get_declared_classes());
?>
Continue reading
Reading time: 1 min
Share:
Written by: rcastera
Resources Scripts

Magento – Show blocks that are not loaded in the current Object or Template

May 25, 2009 1 Comment

Customizing Magento could be difficult. Here is an easy way to display any block in any template file that cannot be loaded with $this->getChildHtml().

PHP
1
<?php echo $this->getLayout()->getBlock('top.search')->toHtml() ?>
Continue reading
Reading time: 1 min
Share:
Written by: rcastera
Resources Scripts

Magento – Find which Class is loaded in the current Template

May 24, 2009 No Comments

If you’ve just started working with Magento, you’ll find that figuring out which class is loaded in the current template file very difficult. Here’s a little snippet that helps you figure it out. Just place the following into any .phtml template files:

PHP
1
<?php print_r(get_class_methods(get_class($this))); ?>

or

PHP
1
<?php print_r($this->debug()); ?>

or

PHP
1
<?php echo Zend_Debug::dump($this); ?>
Continue reading
Reading time: 1 min
Share:
Written by: rcastera
Resources Scripts

Update your Twitter status using PHP

May 13, 2009 18 Comments

Twitter is an awesome service! I use it on a daily basis and have grown addicted to it. Posting to twitter VIA an application is something that I’m sure you’ll need to do at some point. Using this simple little script below, you can post updates to twitter accounts fairly easily. Don’t forget to add your username, password and message below.

Continue reading
Reading time: 1 min
Share:
Written by: rcastera
Resources Scripts

Creating a TinyURL with the TinyURL API

May 8, 2009 22 Comments

The popular URL shortening service TinyURL provides a quick API that creates TinyURL’s on the fly. Here’s how you can access that API.

PHP
1
2
3
4
5
6
<?php
function createTinyUrl($strURL) {
    $tinyurl = file_get_contents("http://tinyurl.com/api-create.php?url=" . $strURL);
    return $tinyurl;
}
?>

Make a call to our function above, and it outputs a new tinyURL.

PHP
1
<?php echo(createTinyUrl('https://www.richardcastera.com/2009/05/09/creating-a-tinyurl-with-tinyurl-api/')); ?>
Continue reading
Reading time: 1 min
Share:
Written by: rcastera
Resources Scripts

WordPress – Embed Adsense in your posts

May 7, 2009 5 Comments

Adding Google Adsense to your blog’s post is easy. No need to download and add another plugin to your WordPress installation. Just follow these easy instructions! First, you have to add the following code to your theme’s functions.php file.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
<?php
function showAds() {
    return '<script type="text/javascript">
    google_ad_client = "pub-2102064382433354";
    google_ad_slot = "5772977330";
    google_ad_width = 468;
    google_ad_height = 60;
   </script>
   <script type="text/javascript" src="https://pagead2.googlesyndication.com/pagead/show_ads.js"></script>';
}
add_shortcode('myadsense', 'showAds');
?>

Saved the functions.php file and upload it. Now you can embed an Adsense unit into your posts easily by pasting the following code in the editor, in html mode:

XHTML
1
[myadsense]
Continue reading
Reading time: 1 min
Share:
Written by: rcastera
Resources Scripts

WordPress – List the most recent comments

May 3, 2009 5 Comments

Easily list the most recent comments in a sidebar or a tab. The code below will list the 10 most recent comments. You can change the number displayed by changing the value of the variable, $intCommentLimit to list more or less comments

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
function getRecentPosts() {
    global $wpdb;
    $intCommentLimit = 10;
    $strSql = "SELECT DISTINCT ID,
                      post_title,
                      post_password,
                      comment_ID,
                      comment_post_ID,
                      comment_author,
                      comment_date_gmt,
                      comment_approved,
                      comment_type,
                      comment_author_url,
                      SUBSTRING(comment_content, 1, 50) AS com_excerpt
               FROM $wpdb->comments
               LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID = $wpdb->posts.ID)
               WHERE comment_approved = '1' ORDER BY comment_date_gmt DESC LIMIT $intCommentLimit";
    $comments = $wpdb->get_results($strSql);
    $strOutput = "<ul>\n";
    foreach ($comments as $comment) {
        $strOutput .= "<li>" . strip_tags($comment->comment_author)  . " Says, <br>" .
                      "<a href="\""" .="" get_permalink($comment-="">ID) . "#comment-" .
                      $comment->comment_ID . "\" title=\"on " .
                      $comment->post_title . "\">" .
                      strip_tags($comment->com_excerpt) . " ...</a></li>\n";
    }
    $strOutput .= "</ul>\n";
    return $strOutput;
}
echo(getRecentPosts());
?>
Continue reading
Reading time: 1 min
Share:
Written by: rcastera
Resources Scripts

PHP – Generate a random password based on length

September 15, 2008 No Comments

Here’s a quick function to generate a password. All you have to do is call it passing the length of your required password as an argument.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
<?php
function generatePassword($intNumOfChars) {  
    if (is_numeric($intNumOfChars) && ($intNumOfChars > 0)) {
    $strChars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_";
    for ($i = 0; $i < $intNumOfChars; $i ++)  {
        $strPassword .= $strChars[rand(0, strlen($strChars)-1)];
     }
    }
    return $strPassword;
}
?>
Continue reading
Reading time: 1 min
Share:
Written by: rcastera
Resources Scripts

PHP – Truncate a string to a given length

September 9, 2008 No Comments

This is a function I use to display the ellipses when I want to truncate and display a summary of a larger body of text.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
function truncateString($intLength = 0, $strText = "") {
    if ($intLength == 0) {
        return $strText;
    }
    
    if (strlen($strText) > $intLength) {
        preg_match("/[a-zA-Z0-9]{0, " . $intLength . "}/", $strText, $strNewText);
        return ($strNewText . "...");
    }
    else {
        return $strText;
    }
}
?>
Continue reading
Reading time: 1 min
Share:
Written by: rcastera
Page 2 of 3«123»

About me

Hello, my name is Richard Castera. I have more than 12 years of experience architecting, implementing, leading and launching large scale, high performance software products in a fast-paced agile environment.

Popular Posts

Magento – Display new products on the home page

May 20, 2009

How to Fix “Internet Explorer Cannot Open the Internet Site- Operation Aborted” Error

May 9, 2009

PHP – Convert Array to Object with stdClass

July 5, 2009

Categories

  • Home Automation
  • Resources
  • Scripts
  • SEO
  • Thoughts
  • Web Server

Tags

Apache Apple Bash CSS Drupal Free home automation htaccess Java Javascript Magento PHP raspberry pi SEO SQL thoughts Wordpress

© 2019 copyright Richard Castera // All rights reserved