Skip to content

Instantly share code, notes, and snippets.

@ruebenramirez
Last active October 21, 2015 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruebenramirez/88978603da73b9657a0e to your computer and use it in GitHub Desktop.
Save ruebenramirez/88978603da73b9657a0e to your computer and use it in GitHub Desktop.
<?php
// create a class `foo` with a member function `do_foo`
class foo
{
function do_foo()
{
echo "Doing foo.";
}
}
$bar = new foo;
$bar->do_foo(); // will print out "Doing foo."
?>
<?php
///////////////////////////////////
// create a class `foo` with a member function `do_foo`
class foo
{
function __construct()
{
echo "created class `foo`";
}
function do_foo()
{
echo "Doing foo.";
}
}
$bar = new foo; // will print "created class `foo`"
$bar->do_foo(); // will print "Doing foo."
?>
<?php
///////////////////////////////////
// constructors are normally used to setup instance data, or configure the instance for however it's going to be used though...
// create a class that takes a user function argument and then prints the user's name (function arguments are also known as parameters)
class foo
{
function __construct($user)
{
$this->user = $user; // $this-> is used to create and reference instance variables (variables that only exist in a given object instance)
echo "created class `foo` for " + $this->user;
}
function do_foo()
{
echo "Doing foo for " + $this->user;
}
}
$user = "rueben";
$bar = new foo($user); // will print "created class `foo` for rueben"
$bar->do_foo(); // will print "Doing foo for rueben"
// so if we created another instance of foo with a different user, it wouldn't use rueben...it would use the other user value
$a_different_user = "shawn";
$baz = new foo($a_different_user); // will print "created class `foo` for shawn"
$baz->do_foo(); // will print "Doing foo for shawn"
?>
@shawnbass85
Copy link

I think I understand one thing. Basically, it is a tool that lets you automate the creation of a specific type of common tool. It's like you're creating users, and a separate account for each user. That way, you can have multiple setups for doing different things. Kind of like creating characters in a game. Each character has their own set of skills and abilities and stats. So this is something you would do in a game's code to make it easy for users to make new accounts, new characters, etc. I just wish I had some problems to do to help me get some practice at doing it by actually creating something. I can't think of anything to create yet. maybe I'll make a text based game.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment