本文整理汇总了PHP中MyClass::setProperty方法的典型用法代码示例。如果您正苦于以下问题:PHP MyClass::setProperty方法的具体用法?PHP MyClass::setProperty怎么用?PHP MyClass::setProperty使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MyClass
的用法示例。
在下文中一共展示了MyClass::setProperty方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setProperty
<?php
class MyClass
{
public $prop1 = "I'm a class property!";
public function setProperty($newval)
{
$this->prop1 = $newval;
}
public function getProperty()
{
return $this->prop1 . "<br />";
}
}
// Create two objects
$obj = new MyClass();
$obj2 = new MyClass();
// Get the value of $prop1 from both objects
echo $obj->getProperty();
echo $obj2->getProperty();
// Set new values for both objects
$obj->setProperty("I'm a new property value!");
$obj2->setProperty("I belong to the second instance!");
// Output both objects' $prop1 value
echo $obj->getProperty();
echo $obj2->getProperty();
示例2: setProperty
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
class MyClass
{
public $prop1 = "I'm a class property!";
public function setProperty($newval)
{
$this->prop1 = $newval;
}
public function getProperty()
{
return $this->prop1 . "<br />";
}
}
$obj = new MyClass();
echo $obj->getProperty();
// Get the property value
$obj->setProperty("I'm a new property value!");
// Set a new one
echo $obj->getProperty();
// Read it out again to show the change
?>
</body>
</html>
示例3: setProperty
public $prop1 = "I'm a class property";
public function setProperty($newval)
{
$this->prop1 = $newval;
}
public function getProperty()
{
return $this->prop1 . "<br />";
}
}
$obj = new MyClass();
$obj2 = new MyClass();
echo $obj->getProperty();
echo $obj2->getProperty();
$obj->setProperty("Son! You're close to becoming a developer");
$obj2->setProperty("Son! Welldone!");
echo $obj->getProperty();
echo $obj2->getProperty();
//$myfile = fopen("webdictionary.txt", "r") or die("Unable to open file!");
//echo fread($myfile,filesize("webdictionary.txt"));
//fclose($myfile);
//Using Class Inheritance
class MyOtherClass extends MyClass
{
public function __construct()
{
/*
Preserving Original Method Functionality While Overwriting Methods
To add new functionality to an inherited method while keeping the original method intact, use the parent keyword with the scope resolution operator (::):
*/
parent::__construct();