Jump to content

Property reassignment


wJesus56

Recommended Posts

<?PHP

class 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; //null

Hello! 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?



 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$ 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.
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

$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!

 

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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!

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.