I'm trying to build a function inside a PHP class, however whenever I invoke the function, I am only returning the first variable.
class Nums
{
private $a = 7;
private $b = 8;
public function sum()
{
return $this->a + $this->b;
}
public function __set($name,$value) {
switch($name) {
case 'a':
return $this->setA($value);
case 'b':
return $this->setB($value);
}
}
public function __get($name) {
switch($name) {
case 'a':
return $this->getA();
case 'b':
return $this->getB();
}
}
private function setA($i) {
$this->a = $i;
}
private function getA() {
return $this->$a;
}
private function setB($i) {
$this->b = $i;
}
private function getB() {
return $this->$b;
}
}
Am I doing something wrong here, because I can't really see what is wrong with this logic.
It's working for me. Here's what i tried and it output 15.
PHP CODE :
<?php
class Nums
{
private $a = 7;
private $b = 8;
public function sum()
{
return $this->a + $this->b;
}
}
$obj = new Nums();
$c = $obj->sum();
echo $c;
?>
OUTPUT :
15
class Nums
{
private $a = 7;
private $b = 8;
public function sum()
{
return $this->a + $this->b;
}
}
$numObj = new Nums();
echo $numObj->sum();
Running this code returns 15 for me
Related
How can'i create a classe calculator witch can do ita differents operations like : addition , multiplication but with this format $test->two()->add()->one() ==> the result is 3.
Can you help me ?
thank you
class Test
{
private $result;
function __construct()
{
$this->result = 0;
}
function one()
{
$this->result = 1;
return $this;
}
function two()
{
$this->result = 2;
return $this;
}
function add()
{
$this->result += $this->result;
return $this;
}
function getResult()
{
return $this->result;
}
}
$test = new Test();
$a = $test->One()->add()->two();
var_dump($a->getResult());
I did this programm but i didn't had the correct response
the result returned is 2 but i must have 3 (1+2)
Here is a solution.
It works on the basis that add() or subtract() doesn't directly carry out any work, it simply sets a "pending" operation, and that one() or two() (I've shortcut that style to key($num) for simplicity though, I think it's better and more flexible as well) actually does the the last operation specified by add() or subtract(), using the number specified in the input.
It works by using PHP's ability to specify a function to call using a string value. Bit hacky but it seems to work.
class Calculator
{
private $result;
private $nextOp;
function __construct()
{
$this->result = 0;
$this->nextOp = "addVal";
}
function key($num)
{
$this->{$this->nextOp}($num);
return $this;
}
function add()
{
$this->nextOp = "addVal";
return $this;
}
function subtract()
{
$this->nextOp = "subtractVal";
return $this;
}
private function addVal($num)
{
$this->result += $num;
}
private function subtractVal($num)
{
$this->result -= $num;
}
function result()
{
return $this->result;
}
}
$test = new Calculator();
$a = $test->key(1)->add()->key(2)->key(3)->subtract()->key(2)->result();
var_dump($a);
This outputs 4.
N.B. It assumes that if you wrote e.g. key(1)->add()->key(2)->key(2) the second call to key(2) would also do an add, because that was the last operation specified (so the result would be 5 in that case), and also the initial operation is always add as well (although I guess you could allow that to be specified in the constructor). I don't know if these assumptions are acceptable in your scenario, you didn't specify what should happen if the user write something like this, or what the class should do with the initial value.
Live demo: http://sandbox.onlinephpfunctions.com/code/0be629f803261c35017ae49a51fa24385978568d
this is my response. It's worked very fine
// Interface des opérations
interface Operation {
public function plus();
public function minus();
public function divededInto();
public function times();
public function doOperation($value);
}
// Class Calculator
class Calculator implements Operation {
private $result;
private $operation;
private $numbers;
public function __construct($numbers) {
$this->numbers = $numbers;
$this->result = 0;
$this->operation = null;
}
//Surcharge de la méthode __call
public function __call($name, $arguments){
$name = strtolower($name);
$this->doOPeration($this->numbers[$name]);
$this->operation = null;
return $this;
}
// Exécution de l’opération
public function doOperation($value){
switch ($this->operation ){
case '+':
$this->result += $value;
break;
case '-':
$this->result -= $value;
break;
case '/':
$this->result = intDiv($this->result,$value);
break;
case '*':
$this->result *= $value;
break;
default : $this->result = $value;
}
}
What do this reference symbol (&) do in &methodName() method?
Is it necessary?
Is this called a reference by method?
class TestClass
{
private $value;
public function __construct()
{
$this->value = 5;
}
public function &methodName(){
return $this->value;
}
}
echo (new TestClass())->methodName(); //Outputs 5;
It means that the method returns a reference. If you then assign that to a reference variable outside the method, updating the variable will update the property.
class TestClass
{
private $value;
public function __construct()
{
$this->value = 5;
}
public function &refMethod(){
return $this->value;
}
public function valueMethod() {
return $this->value;
}
public function printValue() {
echo $this->value . "<br>";
}
}
$c = new TestClass();
$var1 = &$c->valueMethod();
$var1 = 10;
$c->printValue(); // prints 5
$var = &$c->refMethod();
$var = 20;
$c->printValue(); // prints 20
For example I have a class like this
public function data() {
$a = 1;
$b = 2;
$c = 3;
}
public function codeigniter() {
$a, $b, $b //how i can get the value??
}
How can I get the value of $a, $b, $c so that I can use it in codeigniter() function? I use codeigniter? Do I need to add a return;?
I assume the two function are in both class and in a controller class of codeigniter
class Sample extends CI_Controller {
// declare variables
public $a;
public $b;
public $c;
public function __construct() {
// call codeigniter method
$this->codeigniter();
}
public function data() {
$this->a = 10;
$this->b = 11;
$this->c = 12;
}
public function codeigniter() {
// call method data();
$this->data();
echo $this->a; // prints 10
echo $this->b; // prints 11
echo $this->c; // prints 12
}
}
First create a array and store the values in it and return the array.
public function data() {
$data = array();
$data['a']=1;
$data['b']=2;
$data['c']=3;
return $data;
}
Then you can simply call the function and access the returned data.
public function codeigniter() {
$data_value = $this->data();
echo $data_value['a'];
echo $data_value['b'];
echo $data_value['c'];
}
To get more than one variable from a function you must place those variables in an array and return the array
public function data() {
$a = 1;
$b = 2;
$c = 3;
$data = array();
array_push($data, $a,$b,$c);
return $data;
}//data function ends
public function codeigniter() {
$data = $this->data();
//now use $data
}
I need to assign b value in a inside the method onec, but its failing. Please let me know what I am doing wrong here:
<?php
class One {
public $a = 10;
public $b = 20;
public static function onec() {
$this->a = $this->b;
return $this->a;
}
}
echo One::onec();
?>
Use the self keyword. The $this keyword is not accessible under static context. Also, you should make your variables static
Like this..
<?php
class One {
public static $a = 10;
public static $b = 20;
public static function onec() {
self::$a = self::$b;
return self::$a;
}
}
echo One::onec();
You use $this in static function.
http://www.php.net/manual/en/language.oop5.static.php
<?php
class One {
public $a = 10;
public $b = 20;
public static function onec() {
$obj = new One();
$obj->a = $obj->b;
return $obj->a;
}
}
echo One::onec();
Use this code
class One {
public $a = 10;
public $b = 20;
public function onec() {
$this->a = $this->b;
return $this->a;
}
}
$obj = new One();
echo $obj->onec();
I have two classes which depending on each other:
class A
{
public function __construct(B $b)
{
$this->b = $b;
}
}
class B
{
public function __construct(A $a)
{
$this->a = $a;
}
}
And I need to wrap them through Pimple like this:
$c = new \Pimple();
$c['aService'] = function($c){
return new A($c['bService']);
}
$c['bService'] = function($c){
return new B($c['aService']);
}
But unfortunately I get cycling:
Fatal error: Maximum function nesting level of '100' reached, aborting!
Is there any way to reach this cross-dependency without cycling? Or I can use only unidirectional dependencies?
This reminds me of baboushka's
Of course you're bound to get infinite recursion here. Both functions call each other, each time returning a new instance, that is passed the return value of the call to their function-counter part, which in turn calls the function again, which calls the other function again, which calls....
Bottom line: when you have 2 classes that depend on each other from the get-go (__construct), your design is probably flawed.
The way you've defined both constructors, you'll never be able to create an instance of the classes. Simply because you need to instantiate both classes at the same time.
You can't, you simply cannot do that.
Try this:
class A
{
public $b = null;
public function __construct(B $b = null)
{
$this->b = $b;
}
public function setB(B $b = null)
{
if ($b === null)
{
$b = new B($this);//pass A here
}
$this->b = $b;
return $this;
}
}
class B
{
public $a = null;
public function __construct(A $a = null)
{
$this->setA($a);
}
public function setA(A $a = null)
{
if ($a === null)
{
$a = new A($this);//pass B here
}
$this->a = $a;
return $this;
}
}
By setting the default value of the constructor arguments to null, passing an instance has become optional, so now you can do this:
$a = new A;
$b = new B($a);
//or even:
$bFromA = $a->b;
BTW: always declare your properties beforehand. It'll speed up your classes.
Personally, I'd use a getter and a setter, and lazy-load the dependency, but I would keep the constructor as is:
class A
{
//protected, or private. Access this via getter/setter
protected $b = null;
public function __construct(B $b = null)
{
$this->setB($b);
return $this;
}
//setter, allows injection later on
public function setB(B $b = null)
{
$this->b = $b;//allow to set to null
return $this;
}
//getter, lazy-loader:
public function getB()
{
if ($this->b === null)
{//create new instance, if b isn't set
$this->setB(
new B($this)
);
}
return $this->b;
}
}
class B
{
protected $a = null;
public function __construct(A $a = null)
{
$this->setA($a);
return $this;
}
public function setA(A $a = null)
{
$this->a = $a;
return $this;
}
public function getA()
{
if ($this->a === null)
{
$this->setA(
new A($this)
);
}
return $this->a;
}
}
Using Pimple:
$c['aService'] = function($c)
{
return new A;
};
$c['bService'] = function($c)
{
return new B;
};
$b = $c->bService;
$b->getA();//works just fine