The Little Guy Posted December 13, 2009 Share Posted December 13, 2009 I have 2 classes: - Math - Distance in my math class, I have this line: $distance = new Distance($this->query, $this->distances); and this property: public $special = array(); in my Distance class, when it is created, it has a method that is run called "parse". In the parse method, I want to modify the property shown above in Math, and add values to that array. How can I do this? Here is a more advanced version of my code: search.php $math = new Math($_GET['q'], $distances); print_r($math->special); I would like to be able to use $math->special in the code above, but the array is empty... In the following code, I create a new distance class within the class. Math.php class Math{ public $query = ""; public $result; public $distances; public $special = array(); public function __construct($string = "", $dist){ $this->query = $string; $this->distances = $dist; $this->result = $this->parse(); } public function parse(){ if(preg_match("~(".implode("|", $this->distances).")~", $this->query)){ // Create a new distance class: $distance = new Distance($this->query, $this->distances); $this->info = $distance->info; return $distance->final; } } } In the parse method, I would like to modify the special of $math, but I am not sure how, I assume that it is saving it to my $distance instance, instead of my $math instance which I created in search.php how can I modify $math (in search.php) instead of $distance (in Math.php)? Distance.php class Distance extends Math{ public $query = ""; public $distances; public function __construct($string = "", $dist){ $this->query = preg_replace("~,|\.~", "", $string); $this->distances = $dist; $this->final = $this->parse(); } public function parse(){ preg_match_all("~".implode("|", $this->distances)."~", $this->query, $matches); $this->special[0] = $matches[0][0]; $this->special[1] = $matches[0][1]; } } I hope all this makes sense, it is hard for me to explain. Thanks!!! Link to comment https://forums.phpfreaks.com/topic/185018-modifying-a-property-of-an-object-from-a-different-class/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.