wJesus56 Posted July 28, 2017 Share Posted July 28, 2017 <?PHPclass myClass { public $member; public $freevar; public function __construct($x, $y) {//... }public function __set($name, $value) { if ($name =="value") {$this->member="some info";} }public function testfunc($something) { $this->freevar = $something; return new self('x', 'y'); }}$page = new myClass('x', 'y');$page -> testfunc("something") -> value = "some value";echo $page->member; //nullHello! I'm trying to make a class so that its magic method __set changes the value of the member property in the object class myClass when the value value "some value" is assigned to the non-existent property.The property by reference is not overridden, and the method that implies the use of the static property is not appropriate. How to make it work? Quote Link to comment Share on other sites More sharing options...
kicken Posted July 28, 2017 Share Posted July 28, 2017 By returning new self('x','y') you're returning a new separate object that's not related to your original $page object. What happens is you set the value property in that new object, then since there's no references to that object anywhere it gets immediately destroyed. What you probably want to do is return $this to return the current object, not a new object. Also, in the future when posting code, please surround it in tags. Quote Link to comment Share on other sites More sharing options...
wJesus56 Posted July 28, 2017 Author Share Posted July 28, 2017 (edited) Thank you kicen! I and represented approximately. How to build a class so that you can work with it as in the example above at the end? That is to receive not "null" in "member", and "same value"? Edited July 28, 2017 by wJesus56 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 28, 2017 Share Posted July 28, 2017 (edited) You should explain what you want to achieve, not how you think it can be achieved (because that part obviously didn't work out). Show us a concrete example (not a “MyClass” class doing “something”) where the kind of feature you're trying to implement actually makes sense. Because I can't think of any. Edited July 28, 2017 by Jacques1 Quote Link to comment Share on other sites More sharing options...
wJesus56 Posted July 28, 2017 Author Share Posted July 28, 2017 $ Page = new parse ("<input id = id1 value = ''>"); $ Page -> getElementByID ("id1") -> value = "some value"; $ Page -> content; // <input id = id1 value = 'some value'> I want to make a simple parser, and work with it roughly as above, I've already written the replacement method, and I need to have access to the content member (for example) in which there should be a result with a replacement outside the class without using a static property. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 28, 2017 Share Posted July 28, 2017 OK, now we at least know what you want. First off: You realize that there are dozens of well-tested HTML parsers out there, right? PHP itself has DOMDocument. The only reason for implementing your own would be if you're deeply interested in formal grammars and want to explore actual parsing techniques, not just put together a few regex hacks. Otherwise, just use what's already there. In any case, your approach makes no sense. The point of parsing is to build a tree structure where each element is represented by its own node object. When you change an attribute of an element, you only change this one object. The tree itself has nothing to do with attributes. Quote Link to comment Share on other sites More sharing options...
wJesus56 Posted July 29, 2017 Author Share Posted July 29, 2017 I only got one simple dom parcer which I would be comfortable with, but it returns a large string of html code, so I was puzzled to write my simple parser that works a little differently, but still retains the custom formatting. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 29, 2017 Share Posted July 29, 2017 DOMDocument can produce perfectly formatted markup. If you want the kind of formatting you have above with the spaces and unquoted attributes, I suggest you don't use that. The formatting is simply bad, and using a buggy self-made parser just to have bad formatting makes no sense. Quote Link to comment Share on other sites More sharing options...
wJesus56 Posted July 29, 2017 Author Share Posted July 29, 2017 $Dom = new DOMDocument ();$HTML = file_get_contents ('views / file.html');@ $Dom-> loadHTML ('<meta http-equiv = "Content-Type" content = "text / html; charset = utf-8">'. $ HTML);$Dom-> getElementById ("id") -> setAttribute ('value', 'some value');$Dom-> saveHTML (); I solved my task as above, after I got the setAttribute method! Formatting is preserved, in contrast to the Simple Dom!Thank you all for your attention to my question! Quote Link to comment Share on other sites More sharing options...
wJesus56 Posted July 29, 2017 Author Share Posted July 29, 2017 (edited) Is there a method in "DOM Document" to find an element on the id and change its contents, such as with "simple house" or is there a method for parsing a pair?(simple dom variant ) $ Dom-> getElementById ("header") -> innertext = sometext; Edited July 29, 2017 by wJesus56 Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 29, 2017 Share Posted July 29, 2017 (edited) Remember what I said about parsers creating tree structures? You're now dealing with nodes, not text. Google for "php domdocument remove all children". Then create and append the new nodes. Edited July 29, 2017 by Jacques1 Quote Link to comment Share on other sites More sharing options...
wJesus56 Posted July 30, 2017 Author Share Posted July 30, 2017 I found an alternative method in the DOM DOCUMENT to change the content in the layer: $dom->GetElementById('id')->nodeValue = "<p>This is some new text</p>"; Thank you for your attention to my question! Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 30, 2017 Share Posted July 30, 2017 No, this does not change the HTML content. Test it, and you'll see that you end up with <p>This is some new text</p> Why? Because you've created text, not an HTML element node. If you still don't understand the fundamental difference, I'm afraid this project won't be very successful. Quote Link to comment Share on other sites More sharing options...
wJesus56 Posted July 31, 2017 Author Share Posted July 31, 2017 Yes, I noticed, as you behold such a decision? $dom->GetElementById('container-profile')->nodeValue = "<p>This is some new text</p>";$page = $dom->saveHTML();echo htmlspecialchars_decode($page); Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 31, 2017 Share Posted July 31, 2017 This is wrong and stupid and will give you invalid documents. Anyway, I get the impression that you're now more interested in trolling than actually solving the problem. Good luck. Quote Link to comment Share on other sites More sharing options...
wJesus56 Posted July 31, 2017 Author Share Posted July 31, 2017 I understand how the audience at my suggestion to help solve the issue you can't, thank you for your wishes. Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted July 31, 2017 Share Posted July 31, 2017 (edited) I've explained the solution at least three times, so, yes, you are a troll. Or too stupid to follow simple instructions. Edited July 31, 2017 by Jacques1 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.