php类参数
❶ php中class 类 传参的问题;
你这样写运行没报错?!
把 $this->$kk 改成 $this->kk,其他几个地方类似。
❷ php中的数组可以作为参数提供给,php中自己编写的类么
可以的,亲。不过你又语法错误,类不需要加()
还有就是数组的起始应该是0,不是从一开始的。下面的调试过的代码。成功的输出了i,和j的值。
<?php
$shuzu[] = 1;
$shuzu[] = 2;
$shuzu[] = 3;
class myclass{
private $i;
private $j;
public function __construct($shuzu1)
{
$this->i = $shuzu1[0];
$this->j = $shuzu1[1];
echo $this->i;
echo $this->j;
}
}
$myclass1 = new myclass($shuzu);
?>
❸ PHP 类之间传递参数
1. 用继承,B继承A,其中A的mm方法里面的参数k作为方法的返回值,这样就可以给到A去使用了。
classBextendsA{
publicfunctiontt(){$k=$this->mm();}
}
2.在A类里面实例化B类。
classA{
publicfunctiontt(){
$class=newB();
$k=$class->mm();
}
}
两个方法的前提条件都是需要mm方法的k变量作为返回值,然后在A类调用的时候就可以得到这参数了。
3.将k参数储存到A类的属性中。
步骤大致和2方法差不多,也是要在tt方法里面实例化B类,但是B类的mm方法改为:
publicfunctionmm()
{$this->k=$k;}
这样就不用k为返回值了,然后在A类中这样:
classA{
publicfunctiontt(){
$class=newB();
$class->mm();
$k=$class->k;
}
}
不保证代码的完全正确,但是思路是对的。
❹ PHP类成员方法的参数传递
function dosomething()
{
$bb = $this->bb;
$othersomething = $this->othersomething();
...
}
改成这样应该没问题了。
方法的参数是新定义一个变量,注意是新定义,方法结束自动销毁,将成员变量当做参数当然不行。望点赞!
❺ 请问php中类方法名后面括号里参数是什么意思呢如下
这个是参数默认值!
默认值的作用是在调用函数或方法的时候, 如果没提供值, 则会调用参数默认值!
也就是说, 有默认值的函数或者方法, 参数可提供也可不提供!
反之, 必须提供, 不然会出错!
❻ php如何传递类参数
直接实例化类
$db = new db($db_host, $db_user, $db_pass, $db_name, $enable_debugmode);
然后就可以取类里面的对象了
$db -> query();
如果要在其它的页面调用这个类里面的对象的话,可以先把这个类的文件包含进来:
include("class.inc.php")/require("class.inc.php"),然后就可以用上面的方法来调用
❼ php 类中方法之间参数怎么调用
class A
{
public $bb,$cc;
function othersomething()
{
return $this->cc;
}
}function dosomething()
{
$bb = $this->bb;
$othersomething = $this->othersomething();
}方法的参来数是新定义一个源变量,注意是新定义,方法结束自动销毁,
2.PHP类中方法定义的参数与调用时的参数名称可以不同。
带默认值的就是指当这些参数没有给出的时候可以按照预定义内容进行赋值(按参数顺序调用)。
functiontext($i,$a="test1",$test="test2"){
echo"<h1>{$i}</h1>";
echo"<h1>{$a}</h1>";
echo"<h1>{$test}</h1>";
}
2.调用:
text("test");
----显示
test
test1
test2
text("test","test3","test4");
----显示
test
test3
test4
❽ PHP里一个类参数传递问题
要实现你要的功能,其实很简单
class B {
function good() {
// 这是你原来的代码,直接输出了,这个函数没有返回值,因此在前面调用的时候就直接输出了
// echo "very good";
// 改为将内容作为返回值,在调用的地方对返回值处理
return "very good";
}
}
❾ php类方法中参数问题,参数怎么是一个类名 加上变量,怎么理解
是声明这个变量,是“这个类”的类型。
例如 funciton getStudentName( Student $stu){
return $stu.name;
}
这个例子中,就是你问问题的例子, 说的是 获取学生姓名,
这个参数中,声明 变量 $stu 是Student类,其实这个时候$stu是对象,而不是变量。
返回学生的姓名。
❿ php函数设定参数类型
php 函数的参数类型可以指定为类名或数组类型array,比如
这样是对的public function Right( My_Class $a, array $b )
这样是错的public function Wrong( string $a, boolean $b )
如果需要其他类型,需要在函数内部进行类型检查
参考
http://www.php.net/manual/zh/functions.arguments.php
这一段
public function Right( My_Class $a, array $b )
tells first argument have to by object of My_Class, second an array. My_Class means that you can pass also object of class that either extends My_Class or implements (if My_Class is abstract class) My_Class. If you need exactly My_Class you need to either make it final, or add some code to check what $a really.
Also note, that (unfortunately) "array" is the only built-in type you can use in signature. Any other types i.e.:
public function Wrong( string $a, boolean $b )
will cause an error, because PHP will complain that $a is not an *object* of class string (and $b is not an object of class boolean).
So if you need to know if $a is a string or $b bool, you need to write some code in your function body and i.e. throw exception if you detect type mismatch (or you can try to cast if it's doable).