PHP Classes and Objects, part 2

Follow Roger facebooktwittergoogle_pluslinkedinrss

This is part 2 of a series. Click here for Part 1

OK. We have our Person object, and we have 2 instances of it set to $jack and $jill. We have a $gender property set to male. But, our $jill object (instantiated from the Person class) should be female. We need the ability to set the gender when an object is instantiated from the class.  This can be done with a constructor method. A method is nothing more than a function within the context of a class.

A constructor method must be named __construct() – note that is 2 underscores preceding the word “construct”. The __construct() method is not required. If it does exist on the class, when am object is instantiated from that class, the method is automatically called.

public function __construct()
{
     //constructor code here
}

For our example class Person, we want the ability to set the $gender property each time we instantiate the class. We do this through the __construct() method by assigning an argument to the method. Arguments are optional on the __construct() method. Recall how we placed an argument in the var_dump() method. We will do the same, but pass a string instead of a variable when we instantiate the objects.

$jack = new Person('male');
$jill = new Person('female');

Now we have to handle the argument in our __construct() method. In the Person() class remove the value assignment of our $gender property. Then accept the argument in the __construct() method.

<?php

//set up our class
class Person {

     public $gender;
     
     public function __construct($input)
     {
          //do something with $input
     }
}

When an object is instantiated $input will be set to the value of the argument supplied in the instantiation call. We need to set the $gender property to that value.

$this->gender = $input;

I had difficulty getting my head around the concept of $this. From what we have already seen, you know that the code above is accessing the gender property on the $this object. Simply put, $this is a variable that stands in for the instantiated object. In our example we have instantiated $jill and $jack as separate Person() objects. So, on the $jill object $this refers to $jill. And, on the $jack object $this refers to $jack. The same would be true of any object instantiated from the Person() class.

Let’s pull everything together into one short script.

<?php

//set up our class
class Person {

     public $gender;

     public function __construct($input)
     {
        $this->gender = $input;
     }
}

//create 2 objects by instantiating our class twice
$jack = new Person('male');
$jill = new Person('female');

var_dump($jack->gender);
var_dump($jill->gender);

When we instantiate the object as $jack we pass the argument as ‘male’. The class accepts the argument in the __construct() method as $input. The method then sets the class property $gender as the value of $input.

The code above will output:
male
female

Hopefully, this lesson helps you understand PHP objects and classes, how they relate to each other, and gives you a start toward building your own.

Check back for future PHP lessons. Be sure to follow me on Twitter @RogerCreasy, or click the icon above.

Share with your social networks facebooktwittergoogle_plusredditpinterestlinkedinmail

Understanding PHP Classes and Objects

Follow Roger facebooktwittergoogle_pluslinkedinrss

In object oriented programming, or OOP, classes are like factories (real-world factories, not OOP factories. I’ll write more on OOP Factories in the future). These factories define and construct objects. In learning OOP I usually saw classes described as blueprints for objects. But, they are more than just a blueprint; classes do construct objects. In PHP, we create a class using the class keyword (keywords implement built-in PHP functionality). Here is an example of a PHP class:

<?php

class Person {

     //define person here
}

Continue reading

Share with your social networks facebooktwittergoogle_plusredditpinterestlinkedinmail

Beginners Guide to AJAX

Follow Roger facebooktwittergoogle_pluslinkedinrss

What is AJAX?

AJAX is magic. It lets developers retrieve information from the server at runtime without a page reload. This means that a page can change in the background while a user is interacting with the page. The most well-known example is Google Suggest – when you start typing a search query in the Google search field, a drop-down with suggested searches appears. This drop-down is generated using AJAX. Continue reading

Share with your social networks facebooktwittergoogle_plusredditpinterestlinkedinmail

JavaScript Objects

Follow Roger facebooktwittergoogle_pluslinkedinrss

Introduction

In JavaScript an object is a collection of properties. Properties consist of a variable name and its value. A property’s value can be a number, a string, another object, a boolean, an array, or a function. Properties set to equal a function are known as methods. Within JavaScript there are 3 types of objects – objects that are part of the language construct, objects that are part of the browser (like DOM elements), and objects created by your program at runtime.To help make the intangible world of a computer program make sense, think of JavaScript objects like objects in the real world. In the real world objects have a collection of properties. Let’s use a person as an example object. A person has an eye color, hair color, height, weight, etc. These are properties of our person. A person also can walk, talk, run, eat, sleep, etc. These properties are methods – things our person can do. Continue reading

Share with your social networks facebooktwittergoogle_plusredditpinterestlinkedinmail

How to set up a web development computer with Ubuntu 14.04

Follow Roger facebooktwittergoogle_pluslinkedinrss

I have been developing on Ubuntu-based boxes for many years. I finally decided to write a post about how my current development
box is set up. I am running Ubuntu 14.04 LTS (Trusty Tahr) on a old Dell Optiplex 780 upgraded to 8gig RAM. The easiest starting point is to boot into an installer from a USB stick. You can create a bootable USB stick on a PC by following these instructions. Be sure to download the version of Ubuntu that is correct for your computer’s architecture.

Why I chose Ubuntu

I use a PC (and occasionally a Mac) at my day job. I have nothing (well, not TOO much) against Microsoft and Windows. But, I found the Linux community, functionality, and available software to fit my style and tastes. Plus, as a web developer I am working with Linux on the server side on a daily basis. So, I wanted to better understand the OS. And, it is just cool to work in a Linux environment.  In fact, I like Ubuntu so much that I have VirtualBox set up on my work PC with an instance of Ubuntu desktop on it. Continue reading

Share with your social networks facebooktwittergoogle_plusredditpinterestlinkedinmail

I want results NOW!

Follow Roger facebooktwittergoogle_pluslinkedinrss

Our society is a “now” society. We want success now. We want stuff now. We want it all, and we want it now. We want our financials to look good this month or this quarter.

But, do we sacrifice long-term true happiness and true success for the pleasures of the moment? Do we spend time making today successful while ignoring the things that are truly important? Do we drive short-term financial success, but not keep our clients’ best interest in mind?

What is really important about the now is how what we are doing now fits into our long-term plan. Long-term success usually requires short-term sacrifice. Maybe we don’t take that expensive vacation because the money needs to be working for our retirement. Maybe we don’t sell the client that highly profitable product that is not right for him, because we need our client to be successful for next year’s sales.

What are you doing now that is an investment into your future?

Share with your social networks facebooktwittergoogle_plusredditpinterestlinkedinmail

Be Prepared for the Tough Questions

Follow Roger facebooktwittergoogle_pluslinkedinrss

Regardless of your role, you will be challenged with tough questions. If you are a salesperson, bigstock-D-Question-Mark-1317415those questions will come from your clients. As a leader, the questions will come from peers, your superior or board, or even from your team. How do you handle questions that challenge your position?

It is important that you completely understand what is being asked. The question may be ambiguous. Or, it may not be the real question; what is actually being asked or the challenge to your position may lie beneath the surface of what is asked. The best way to completely understand the challenge is to repeat the question back; use your own words and restate the question a different way. Continue reading

Share with your social networks facebooktwittergoogle_plusredditpinterestlinkedinmail

3 Questions to Lead Your Team to Success

Follow Roger facebooktwittergoogle_pluslinkedinrss

Micromanaging your team can lead to success. However, if you want a team that functions at a high level and with creativity, you must give your team some autonomy. Instead of dictating how you want a project to be completed, describe, in detail, the results you want. Your role as a leader is provide your team with what they need, and to remove obstacles – make sure you are not one of those obstacles.

Instead of constantly checking up on your team’s progress, ask these questions:

  • What do you need to complete this project?
  • What is in the way?
  • How can I help?

Make sure you engage in conversation with your team. Notice that the questions above are open-ended; they open the door for conversation. Look for ways to help, but stay out of the way. Your team will appreciate your confidence in them.

Related posts:

Share with your social networks facebooktwittergoogle_plusredditpinterestlinkedinmail

Ease your workload – don’t do the wrong things

Follow Roger facebooktwittergoogle_pluslinkedinrss

Many leaders have a hero complex. They want to be known as a problem-solver, the go-to person when something needs to be done or fixed. But, is being that hero in the best interest of your organization? Is handling a problem that could be handled just as well by someone else the best way for you to contribute to the good of your organization?

Defer projects better handled by someone else, to that someone else. As a leader, you should work strategically on the objectives that move you, your team, and your organization toward predetermined goals. Don’t do the wrong things.

To make the decision whether or not to defer a request, ask yourself these questions:

  • Could someone else handle this project better than I can, or least handle it sufficiently?
  • Does this request better fit the responsibilities of someone else?
  • Does this project take me away from something core to my strategic goals or my part in my organization’s strategic goals?
  • Is handling this request the best use of my time?

If the request is coming from a superior, the questions above will help you justify your deflection. Your boss does not want to take you away from your highest value activities.

Being a hero and solving problems is great. But, don’t let your hero complex and doing the wrong things get in the way of what really matters.

 

Share with your social networks facebooktwittergoogle_plusredditpinterestlinkedinmail

Opportunity Knocks. Are you ready?

Follow Roger facebooktwittergoogle_pluslinkedinrss

Opportunity knocks often. Are you prepared to take advantage of it?

Some people seem to be lucky. These people go from one success to another. But, is it really luck? Do they carry a talisman of some sort?

If these people are lucky, it is because they set themselves up to be lucky. Opportunities abound. But, you need to be ready to take advantage of it when it arrives.

  • Practice – You have likely heard, “practice makes perfect”. Work to continuously improve.  You cannot take advantage of opportunity if you aren’t prepared.
  • Be persistent – Keep at it until your soul is no longer in it.
  • Stay informed – You won’t answer the knock if you don’t hear it.
  • Play to Win – You can’t merely show up. Show up planning to win.
  • Listen for the knock – Expect to find  opportunity. Pay attention and look for opportunity in your experiences.

 

Share with your social networks facebooktwittergoogle_plusredditpinterestlinkedinmail