0
class phpParser{

public $pageString;
//some other properties and methods



class software{
    private $baseName;
    private $url;
    private $html;

    public initiateValues (){
        $this->html = new phpParser ();
    }

}


// Now how to access properties and methods of phpParser class through software class member $html (which is actually object // of  phpParser class). I think i should be something like this

$mySoftware = new software();
$mySoftware->html->pageString; //this not work how i access please help me please urgently i am pending my work untill you //guys answer me
4
Contributors
5
Replies
25
Views
3 Years
Discussion Span
Last Post by veedeoo
0

In addition to the change to public, which also allows you to write to the property, you could write a 'get' method, which would allow you to read the property, but not write to it, so you could still keep your properties as private.

e.g.

public function get_property($property)
{
    return (isset($this->$property)) ? $this->$property : NULL;  
}

So..

$pageString = $mySoftware->html->get_property("pageString");

I'm still an OOP novice, so I'm open to be corrected.

0

You cannot do this

$mySoftware = new software();
$mySoftware->html->pageString;

eventhough if we corrected it to

$mySoftware = new software();

Still this will not work

$mySoftware->html->pageString();

the only time it will work if you extend the class parser as in

class software extends phpParser{

    public function __construct(){
        parent::__construct();

    }

otherwise, you have too many choices from abstract method to magic methods.

If the pagestring can do its job without any dependency to any other methods of the phpParser class, then pageString method can be static.

class ClassOne{


    public static function method_one($string){

        return (strtoupper($string));

        }
}        

class ClassTwo{

    public function use_method_one($string){

         return ClassOne::method_one($string);
    }

    }

$objectTwo = new ClassTwo();
echo $objectTwo->use_method_one('hello world'); 

However, if that method is dependent to any other methods, then the object of the distant class must be intialized

class phpParser{

    public $pageString;

    public function __construct(){

    }

    public function parser_method($string){
        return (strtoupper($string));
    }

}


class software{

    private $baseName;
    private $url;
    private $html;

    public function __construct(){

        //$parserObject = new phpParser();
    }

    public function initiateValues ($string){

        $this->html = new phpParser ();
        return($this->html->parser_method($string));
    }
}

$sObject = new software();

echo $sObject->initiateValues('this is from parser_method()');

If an object of the distant class is needed throughout the initialized object, then the object of the distant class must be created during the class initialization.

public function __construct(){

        $this->html = new phpParser();
    }

and

public function initiateValues ($string){

        return($this->html->parser_method($string));
 }

if the method initiateValues() will be called by other methods in the class, then singleton or factory is recommended to make sure there is only one object created of the remote class. This will at least help preserve you server resources from having too many instances of the same object.

Edited by veedeoo: more info added

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.