Jump to content

the class of php


runeveryday

Recommended Posts

i have read some tutorials about the class of php.but i can't understand it clearly.a friend of mine give me an instance.

class username(){ 

      var $username;

    function printName(){

    echo $this->username;

    } 

  function setName($name){

  $this->username = $name;

    }

}

$object = new username;

$object->setName('runeveryday');

echo "My name is : " . $object->getName();

 

i konw state a class is

class classname{ }

but in my friend's example the form is

class classname(){ }

is it right?

who can give me the details about the class.

Any hint would be GREATLY appreciated.

 

Link to comment
https://forums.phpfreaks.com/topic/169322-the-class-of-php/
Share on other sites

Actually, looking at it again, the code is a little messed up. Should be....

 

class username {
   
  private $username;

  function getName(){
    return $this->username;
  } 

  function setName($name){
    $this->username = $name;
  }
}
$object = new username;
$object->setName('runeveryday');
echo "My name is : " . $object->getName();

Link to comment
https://forums.phpfreaks.com/topic/169322-the-class-of-php/#findComment-893489
Share on other sites

Its all pretty well covered in the manual and many other tutorials on the net. If you can't yet understand that basic example, I'm probably not going to be able to explain it in a quick post, I'll try though.

 

Firstly, you have a class.

 

class username {
}

 

In order for this class to actually store a variable within itself, you need to declare that variable.

 

class username {
  private $name;
}

 

We use the keyword private here because we only want that variable available within the class itself.

 

Now, because that variable is only available within that class, we need to create a function (method) to set that variable to something....

 

class username {
  private $name;

  function setName($var) {
    $this->name = $var;
  }
}

 

The $this keyword is used to address a variable (or a method) within the object (a class becomes an object once instantiated, more on this later) itself.

 

We then need a method to access our $name variable from outside the class.

 

class username {
  private $name;

  function setName($var) {
    $this->name = $var;
  }

    function getName() {
      return $this->name;
  }
}

 

Thats it, the entire class. Now to the client code (the code that makes use of the class).

 

// firstly we instantiate the class and assign the returned object to a variable.
$obj = new username;
// we can now set the username variable stored within our object.
$obj->setName('foo');
// and now we can retrieve it.
echo $obj->getName();

Link to comment
https://forums.phpfreaks.com/topic/169322-the-class-of-php/#findComment-893501
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.