我完全是 PHP OOP 的初学者,正在尝试学习 PHP 可见性修饰符。
在 private 修饰符中,我可以从它自己的类中访问私有(private)属性和方法。
但是您可以看到我的代码,我可以从名为 VisibilityModifierChild 的子类访问私有(private)属性。请查看我的代码注释以获得更好的理解,或者您可以在在线 PHP 解释器中运行代码以获取更多信息。
class VisibilityModifier {
public $pub_variable = "public property";
protected $pro_variable = "protected property - <strong>getPro() method</strong>";
private $pri_variable = "private property - <strong>getPri() method</strong>";
//set getter for protected variable
public function getPro() {
return $this->pro_variable;
}
//set getter for private variable
public function getPri() {
return $this->pri_variable;
}
}
class VisibilityModifierChild extends VisibilityModifier {
public function setPubVariable($set) {
$this->pub_variable = $set;
}
public function setProVariable($set) {
$this->pro_variable = $set;
}
// cannot set the value but it does't give any warning and error -- sound cofusing
public function setPriVariable($set) {
$this->pri_variable = $set;
}
// set and get a private property from child class -- sound confusing
public function getPrivateByChild ($set) {
$this->pri_variable = $set;
return $set;
}
}
$visible = new VisibilityModifier();
$visible_child = new VisibilityModifierChild();
echo $visible->pub_variable . "<strong style=\"color:green\">-Looks Good</strong><br>";
$visible_child->setPubVariable("child access public property");
echo $visible_child->pub_variable . "<strong style=\"color:green\"> -Looks Good</strong><br>";
echo '<hr>';
// echo $visible->pro_variable . "<br>";
echo $visible_child->getPro() . "<strong style=\"color:green\"> -Looks Good</strong><br>";
$visible_child->setProVariable("child access protected property<strong style=\"color:green\"> -Looks Good</strong>");
echo $visible_child->getPro() . "<br>";
echo '<hr>';
// echo $visible->pri_variable . "<br>";
echo $visible->getPri() . "<strong style=\"color:green\"> -Looks Good</strong><br>";
$visible_child->setPriVariable("child access private property") . "<br>";
echo $visible_child->getPri() . "<strong style=\"color:red\"> -sound confusing</strong><br>";
echo $visible_child->getPrivateByChild("child can access private property<strong style=\"color:red\"> -sound confusing</strong>") . "<br>";
最佳答案
转储子实例时,您会看到现在有两个属性“pri_variable”:
object(VisibilityModifierChild)#2 (4) {
["pub_variable"]=>
string(15) "public property"
["pro_variable":protected]=>
string(53) "protected property - <strong>getPro() method</strong>"
["pri_variable":"VisibilityModifier":private]=>
string(51) "private property - <strong>getPri() method</strong>"
["pri_variable"]=>
string(29) "child access private property"
}
您无权访问的未更改的私有(private)属性,以及您在调用 setter 时动态设置的私有(private)属性。
由于子类看不到私有(private)属性,它只会在自己的实例范围内创建一个同名的新属性 (this)。看起来好像您正在访问私有(private)属性(property),但您实际上是在创建一个新属性(property)。私有(private)属性不变。
关于php 可见性修饰符混淆部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46766380/
我正在编写一个方法,它将在一个类中定义一个实例方法;类似于attr_accessor:classFoocustom_method(:foo)end我通过将custom_method函数添加到Module模块并使用define_method定义方法来实现它,效果很好。但我无法弄清楚如何考虑类(class)的可见性属性。例如,在下面的类中classFoocustom_method(:foo)privatecustom_method(:bar)end第一个生成的方法(foo)必须是公共(public)的,第二个(bar)必须是私有(private)的。我怎么做?或者,如何找到调用我的cust
我在Ruby中遇到了一个关于Dir[]和File.join()的简单程序,blobs_dir='/path/to/dir'Dir[File.join(blobs_dir,"**","*")].eachdo|file|FileUtils.rm_rf(file)ifFile.symlink?(file)我有两个困惑:首先,File.join(@blobs_dir,"**","*")中的第二个和第三个参数是什么意思?其次,Dir[]在Ruby中有什么用?我只知道它等价于Dir.glob(),但是,我对Dir.glob()确实不是很清楚。 最佳答案
我想使用部分字符串搜索数组,然后获取找到该字符串的索引。例如:a=["Thisisline1","Wehaveline2here","andfinallyline3","potato"]a.index("potato")#thisreturns3a.index("Wehave")#thisreturnsnil使用a.grep将返回完整的字符串,使用a.any?将返回正确的true/false语句,但都不会返回匹配的索引找到了,或者至少我不知道该怎么做。我正在编写一段代码,该代码读取文件、查找特定header,然后返回该header的索引,以便它可以将其用作future搜索的偏移量。如果
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visitthehelpcenter指导。关闭9年前。我来自C、php和bash背景,很容易学习,因为它们都有相同的C结构,我可以将其与我已经知道的联系起来。然后2年前我学了Python并且学得很好,Python对我来说比Ruby更容易学。然后从去年开始,我一直在尝试学习Ruby,然后是Rails,我承认,直到现在我还是学不会,讽刺的是那些打着简单易学的烙印,但是对于我这样一个老练的程序员来说,我只是无法将它
K伙计们,所以我创建了这个赞成/反对的投票脚本(基本上就像stackoverflow上的那个),我试图向其中添加一些Ajax,这样页面就不会在您每次投票时都重新加载。我有两个Controller,一个叫grinder,一个叫votes。(磨床基本都是帖子)所以这是所有研磨机的索引(看起来像这样)这是该页面的代码。Listinggrinders"grinders/grinders")%>这就是我在views/grinders/_grinders.erb中的内容true)do|u|%>grinder.id%>"up"%>'create')%>true)do|d|%>grinder.id%>
如thisanswer中所述,在Ruby2.1或更高版本中,此代码:classSimpleTestprivatedefine_method:foodo42endend将定义foo作为SimpleTest的私有(private)方法实例。(在Ruby2.0和更早版本中,它不会是私有(private)的。)但是,我希望做一些不那么琐碎的事情。我想定义一个类可以扩展的DSL,并希望DSL在内部定义的方法尊重调用上下文的私有(private)/protected可见性。这可能不是很清楚,所以这里有一个例子:moduleDsldefhas_a(name)define_methodnamedo42
我将restclient用于多部分表单,以将数据发送到restfulweb服务(它是Panda视频编码服务)。不过,诀窍在于我传递给restclient(Technoweenie分支)的文件来自用户提交的我自己的表单。那么,让我们来看看这个。用户将文件发布到我的Rails应用程序。在我的Controller中,它从params[:file]接收文件。然后我想使用RestClient将params[:file]传递给Panda。我在Panda服务器上遇到的错误如下。我注意到堆栈跟踪中的文件参数也在一个字符串中(我假设Panda将其转换为字符串以获得更好的堆栈跟踪)。~Startedreq
我在标准rails2.1项目中使用Test/Unit。我希望能够独立于任何特定的Controller/操作来测试分部View。好像ZenTest'sTest::Rails::ViewTestCase会有所帮助,但我无法让它工作,与view_testhttp://www.continuousthinking.com/tags/view_test类似Google出现的大部分内容似乎都已经过时了,所以我猜它并不真正适用于Rails2.1非常感谢任何帮助。谢谢,罗兰 最佳答案 我们正在使用RSpec在我们的Rails2.1项目中,我们可以做
请有人帮助我了解ruby应用程序如何管理应用程序的gemfile和rvmgemsets。如果我当前使用的是Gemset,安装了一堆gem,并且我的gemfile中也有gems,那么Ruby应用程序是使用gemfile中的gem还是应用程序的gemset中的gem? 最佳答案 要理解这一点,您需要退后一步,了解rubygems的一般工作原理。让我们从一个没有rvm或Gemfile的系统开始。当您通过“geminstall”安装gem时,它会进入系统gem位置。每当您编写ruby脚本并需要gem时,它就会从那里获取。现在假设
我需要一些在ruby(1.8.6或1.8.7而不是1.9)中实现curry函数的示例。 最佳答案 下面是如何用block而不是方法来柯里化(Currying):defcurry(&block)arity=(block.arity>=0)?block.arity:-(block.arity+1)#returnanimmediatevalueiftheblockhasonereturnblock[]ifarity==0#otherwise,curryitargumentbyargumentargs=[]innermost=lambd