'PHP detect when class is called
I want to create the class once. However, I want the $contents array to be reset when the class method is called on the variables.
I can do this by recreating the class each time, but is there a different solution? How can I trigger if the class is called in any way (method etc.).
<?php
class Page
{
public $contents = array();
public function add_content($content)
{
array_push($this->contents,$content);
return $this;
}
}
$page = new Page();
$ex = $page->add_content('example');
$ex2 = $page->add_content('example2');
?>
OUTPUT
print_r($ex);
print_r($ex2);
Page Object
(
[contents] => Array
(
[0] => example
[1] => example2
)
)
Page Object
(
[contents] => Array
(
[0] => example
[1] => example2
)
)
What i want to do
print_r($ex);
print_r($ex2);
Page Object
(
[contents] => Array
(
[0] => example
)
)
Page Object
(
[contents] => Array
(
[0] => example2
)
)
Solution 1:[1]
I have solved your problem in this way, please let me know if I have misunderstood in any case so, I can fix it for you.
here is my example and I will explain how it works:
<?php
class Page
{
public $contents = array();
private $objectName = "";
public function add_content($content)
{
array_push($this->contents,$content);
return $this;
}
public function add_content2($content, Page $page){
if ($this->objectName === $page->objectName){
array_push($this->contents,$content);
}else{
$this->contents = null;
$this->contents[] = $content;
}
return $this;
}
public function __construct($objectName)
{
$this->objectName = $objectName;
}
}
$page = new Page("page1");
$page2 = new Page("page2");
// I want to add [example, example2] content to same page.
$ex = $page->add_content2('example', $page);
$ex = $page->add_content2('example2', $page);
// I want to add [example2] content to same page2.
$ex2 = $page2->add_content2('example2', $page2);
// I want to add [example2] content to same page2 via page
// this will reset the array and push the last
$ex3 = $page2->add_content2('example2', $page);
echo "<pre>Page 1: \n";
print_r($ex);
echo "</pre>";
echo "<pre>Page 2: \n";
print_r($ex2);
echo "</pre>";
echo "<pre>Page 3: \n";
print_r($ex3);
echo "</pre>";
?>
**My Solution, ** to keep track of which object has been called, I have created a variable so each page has a unique name, and then I will pass the object to the add_content method
private $objectName = "";
each object has to have Name passed to the __construct:
public function __construct($objectName)
{
$this->objectName = $objectName;
}
$page = new Page("page1");
$page2 = new Page("page2");
Now, when I want to add new content to the page, we will check if it is the same in the add_content2 method:
public function add_content2($content, Page $page){
if ($this->objectName === $page->objectName){
array_push($this->contents,$content);
}else{
$this->contents = null;
$this->contents[] = $content;
}
return $this;
}
if it is the same, we can add it, if not we will reset the array contents and last we will add the content
please let me know if you don't understand. here is my local result:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |


