Jump to content

[SOLVED] about setmethod


rsalumpit

Recommended Posts

A method that will set a class property (variable). Getters are the opposite to setters, they get the value of a class property. Setters and getters are magic methods in php5 __set() __get() used for object overloading. Example:

<?php
class foo {
  private $data;

  public function __set($key, $val) {
     $this->data[$key] = $val;
  }

  public function __get($key) {
     return $this->data[$key];
  }
}
$x = new foo();
// set a value
$x->set('name', 'joe');
// get a value
print $x->get('name');
?>

A method that will set a class property (variable). Getters are the opposite to setters, they get the value of a class property. Setters and getters are magic methods in php5 __set() __get() used for object overloading. Example:

<?php
class foo {
  private $data;

  public function __set($key, $val) {
     $this->data[$key] = $val;
  }

  public function __get($key) {
     return $this->data[$key];
  }
}
$x = new foo();
// set a value
$x->set('name', 'joe');
// get a value
print $x->get('name');
?>

 

so how can i apply it here this my homework from school setmethod is a new subject for us..thnx for the help man

Well, I'm not sure it could be explained any simpler.

 

Given your class with two properties, you simply need to make to seter methods to set each property.

 

class Div {
  
  private $nom;
  private $denom;
  
  public function setnom($amount)
  {
    $this->nom = $amount;
  }

  public function setdenom($amount)
  {
    $this->denom = $amount;
  }
  function do_it()
  {
    return $this->nom/$this->denom;
  }
}

$div = new Div;
$div->setnom = 100;
$div->setdenom = 50;
echo $div->do_it();

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.