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');
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.