热搜:NVER node 开发 php

问个问题.

2024-09-13 18:55:01
问个问题.

<?phpclass Person {    var $name;    var $age;    function say() {        echo "我的名字叫:".$this->name."
"; echo "我的年龄是:".$this->age; }}

<?php     class cal{        public function sum($a,$b){            return $a+$b;        }        public function prt($a,$c){            return $a*$c;        }        public function result($a,$b,$c){            $a=$this->sum($a,$b);            return $this->prt($a,$c);        }    }    $c=new cal();    echo "(2+3)*10= " .$c->result('2','3','10');?>


能详细解答一下吗?不太理解 $this->prt()括号里可以var mixed吗?对->也不太理解!


回复讨论(解决方案)

能详细解答下吗!

$this 对象自己

比如你
$c=new cal(); //实例化类cal,这时 $c 就是对象了
echo "(2+3)*10= " .$c->result('2','3','10'); //你可以这样访问 $c 这个对象

那么在 $c 内部你该如何访问呢?
答案就是用 $this

$this->prt() 里面应该不能放。。
在执行到$a*$c的时候会报错。。
Unsupported operand types
意思是数组和数字不可相乘吧。
$this 代表这个实例化的对象本身。
这个对象里包括prt()方法。
$this->prt() 意思就是执行这个对象本身包含的prt()
->大致就是 选取和执行的意思。

$this->prt() 里面应该不能放。。
在执行到$a*$c的时候会报错。。
Unsupported operand types
意思是数组和数字不可相乘吧。
$this 代表这个实例化的对象本身。
这个对象里包括prt()方法。
$this->prt() 意思就是执行这个对象本身包含的prt()
->大致就是 选取和执行的意思。
貌似可以放,没有报错

$this 对象自己

比如你
$c=new cal(); //实例化类cal,这时 $c 就是对象了
echo "(2+3)*10= " .$c->result('2','3','10'); //你可以这样访问 $c 这个对象

那么在 $c 内部你该如何访问呢?
答案就是用 $this
通俗明白thks

是吗?
那这段代码你可以执行吗?我是报错的。
可以运行的话麻烦告诉下我结果。

<?php$a = array(1,2);    class cal{        public function sum($a,$b){            return $a+$b;        }        public function prt($a,$c){            return $a*$c;        }        public function result($a,$b,$c){            $a=$this->sum($a,$b);            return $this->prt($a,$c);        }    }    $c=new cal();    echo "(2+3)*10= " .$c->result($a,'3','10');?>

是吗?
那这段代码你可以执行吗?我是报错的。
可以运行的话麻烦告诉下我结果。

<?php$a = array(1,2);    class cal{        public function sum($a,$b){            return $a+$b;        }        public function prt($a,$c){            return $a*$c;        }        public function result($a,$b,$c){            $a=$this->sum($a,$b);            return $this->prt($a,$c);        }    }    $c=new cal();    echo "(2+3)*10= " .$c->result($a,'3','10');?>

这段代码不能运行,但
<?php     class cal{        public function sum($a,$b){            return $a+$b;        }        public function prt($a,$c){            return $a*$c;        }        public function result($a,$b,$c){            $a=$this->sum($a,$b);            return $this->prt($a,$c);        }    }    $c=new cal();    echo "(2+3)*10= " .$c->result('2','3','10');?>
这段代码可以运行 我帖子的这段

那当然可以运行了。
我心里的var mixed
  int     整型
  longint 长整型
  real    实数型
  char    字符型
  string  字符串
  array   数组
不知道是不是我误解了LZ问的意思

那当然可以运行了。
我心里的var mixed
  int     整型
  longint 长整型
  real    实数型
  char    字符型
  string  字符串
  array   数组
不知道是不是我误解了LZ问的意思
哦没有,我理解错了 谢谢你的回答thanks!