Class Basic?


<?php
class Details
{
var $property1;
var $property2;

function display($name, $age)
{
print "Your name is : $name<br>";
print "Your age is: $age<br>";
}
}

$obj = &new Details;
$obj->name = 'Jamie';
$obj->age = '13';
$obj->display($obj->name, $obj->age);

?>


Is This Right :confused:

Hi Jamie, welcome to SitePoint! :smiley:

Well, that would depend on what you’re planning on doing.

How does this look?


<?php
class User
{
  public $forename;
  public $surname;
}

class UserPrinter
{
  public static function display(User $user){
    echo 'Hello ', $user->forename, ', your surname is ', $user->surname;
  }
}

$me = new User;
$me->forename = 'Anthony';
$me->surname = 'Sterling';

UserPrinter::display($me);

/*
  Hello Anthony, your surname is Sterling
*/

What material are you using, have you tried the manual ? If you’re struggling with PHP’s OOP features, maybe you should start a little lower down and work your way up. :wink:

A more usual setup is something like this:


<?php
class Details
{
  private $name;
  private $age;

  public function __construct($name, $age)
  {
     $this->name=$name;
     $this->age=$age;
  }
 
  function display()
  {
    print "Your name is : $this->name<br>";
    print "Your age is: $this->age<br>";
  }
}

$obj = new Details('Jamie', 13);
$obj->display();
?>

and how would i make this work with using gphpedit

oh ok thanks.
:slight_smile:

where would i find any php applications to run it

What operating system are you using Jamie? If you’re using a Windows machine, you can install ‘wampserver’, this will install a full web server for you.

Lorna Jane Mitchell recently posted a presentation on [URL=“http://www.slideshare.net/lornajane/object-oriented-programming-in-php”]Basic PHP OOP over at SlideShare, hopefully they can help you too.

im using linux mint 10

Hi Jamie,

I see you’re using gphpedit, which is a *nix app I believe. If you’re using Linux Mint or Ubuntu, you will need to install PHP first.

To get php to run in the Terminal, you will need to issue the following commands - in order:-


sudo apt-get update


sudo apt-get install php5-cli

Once this is complete, you can run a PHP script like so…


sudo clear && sudo php -f path/to/php/file.php

For example, if your php script was saved in your Documents folder as script.php, you would execute…


sudo clear && sudo php -f ~/Documents/script.php

ive already done php5 and thanks for the help :slight_smile:

So, is it working? :slight_smile:

its saying invalid operation - - “f”

Save a script called script.php in your Documents folder, and in that script put the following…


<?php
echo 'Hello Jamie, today is ', date('l'), '.';

Then (copy/paste ?), execute the following command in the terminal…


sudo clear && sudo php -f ~/Documents/script.php

You should get…


Hello Jamie, today is Wednesday.

sudo: invalid option – ‘f’
usage: sudo -h | -K | -k | -L | -V
usage: sudo -v [-AknS] [-p prompt]
usage: sudo -l[l] [-AknS] [-g groupname|#gid] [-p prompt] [-U username] [-u
username|#uid] [-g groupname|#gid] [command]
usage: sudo [-AbEHknPS] [-C fd] [-g groupname|#gid] [-p prompt] [-u
username|#uid] [-g groupname|#gid] [VAR=value] [-i|-s] [<command>]
usage: sudo -e [-AknS] [-C fd] [-g groupname|#gid] [-p prompt] [-u
username|#uid] file …

not open input file

Are you sure you’re copying the command I posted above exactly?

Ok, where did you save the file? Are you following these steps ?

ok im following the exact steps and i’ve saved it in documents and its still saying no input file

ok doesnt matter im thinking it automatically saves it as php ok ive got it to work now