[英]PHP Using class property into another class and use the method again
我不知道如何稱呼它為技術術語。
我有一個具有$content
屬性的base
類,該屬性包含整個頁面數組元素。 由於某些限制,我無法擴展base
類
/**
* This is the main class
*
* Class base
*/
class base {
/**
* Holding entire page data in nested array. Such as nav item, posts, metadata etc. with the respective key => []
* Example:
* $this->content = [
* 'head' => [ // some body elements such as meta tags, title etc],
* 'body' => [
* ]
* ]
*
* @var array
*/
public $content = [];
/* This is just for an example of array holding values */
public function some_methods() {
// various method to process the $content and return the element
$this->content = [
'head' => [ /* some body elements such as meta tags, title etc */ ],
'body' => [
'footer' => [ /* header elements */ ],
'nav' => [ /* nav items */ ],
'article' => [
'text' => [ /* text and title */],
'metadata' => [ /* metadata */]
],
'footer' => [ /* footer elements */ ]
]
];
}
public function load_navs( $navs ) {
$one = new one();
$one->hello($navs);
// OR
// some methods has loops in the `base` class
foreach ( $navs as $key => $value ) {
// whatever..
$one->hello($key, $value);
}
}
public function load_footer( $footer ) {
$two = new two();
$two->widget($footer);
}
}
我要做的是使更多的類自定義base
類methods
某些邏輯。 為此,我需要將base
類的$content
用作另一個類。 它們是我將在base
類methods
使用的新創建類的methods
/**
* Class to customize or override base class logic
* Class one
*/
class one {
// to do some process I need to access $content from the `base` class. For example
public function hello($navs){
// here something to check from `nav`
// this method I will use in base class
if ($this->content['nav']['item']['selected']){
// do the stuff
}
}
}
/**
* Class to customize or override base class logic
* Class two
*/
class two {
// to do some process I need to access $content from the `base` class. For example
public function hello($footer){
// here something to check from `footer`
// this method I will use in base class
if ($this->content['footer']['widget']['type'] == 'foo'){
// do the stuff
}
}
}
如果您不能擴展您的類,則可以使用靜態屬性。 靜態屬性是指向類而不是實例化對象的屬性鏈接。
請查看此鏈接以獲取詳細信息 。 我復制過去的重要信息
將類屬性或方法聲明為靜態使其可以訪問,而無需實例化類。
因此,您可以在基類中聲明static $content
,並像這樣使用它: base::$content;
class Base {
public static $content = [];
}
class One {
function foo () {
var_dump(Base::$content);
}
}
$foo = new One();
$foo->foo();
//顯示:數組(size = 0)空
按照約定,您的名字Classe需要大寫(Base,One,Two)
編輯:沒有靜態,沒有擴展。
/ *如果您不能擴展並且不能使用靜態,則可以實例化One Class中的Base對象。 就像Tobias在此評論中說的那樣,如果要同時更改Base和One的$ content,則需要通過引用傳遞var。 看第一個示例,在一個工作之前和之后,base-> $ content為空。 再看第二個示例:我通過引用獲得基本內容,並且兩種情況下的內容都發生了變化。 我的英語水平可能太差了,所以我建議您閱讀該文檔以供參考。 * /
class Base {
public $content = [];
function displayContent() {
var_dump($this->content);
}
}
class One {
function displayContent() {
$base = new Base();
$base->content = 'toto';
var_dump($base->content);
}
}
$base = new Base();
$one = new One();
$one->displayContent();
$base->displayContent();
// $one->content == 'toto'; but $base->content still empty.
現在:參考:
class Base {
public $content = [];
function displayContent() {
var_dump($this->content);
}
}
class One {
public $content;
function displayContent() {
var_dump($this->content);
}
}
$base = new Base();
$one = new One();
$one->content = &$base->content;
$one->content = 'toto';
$one->displayContent();
$base->displayContent();
// NOW, $one->content == 'toto'; and $base->content = 'toto'
在繼承基類的屬性時,請使用以下提到的行。
class One extends Base
class Two extends Base
比之后可以在子類中使用基類的屬性
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.