June 20, 2013

Access an internally used list

Pulz’s Question:

I’m coding the Dijkstra algorithm in Java. My first method

public void populateDijkstraFrom(Node startNode)

creates a Linked List of nodes with its respective distances and predecessors. My second method

public List<Node> getShortestPath(Node startNode, Node targetNode)

is supposed to create a list of nodes with the shortest path from the startNode to the targetNode using the list of nodes from my populateDijkstraFrom method.

However, I don’t know how to access the list of nodes from my Dijkstra method in the getShortestPath method. I could change the return type from void to LinkedList but I was told that it works using void.

How do I do it?

Thanks

There are basically two ways of solving this.

Easiest one would be return the list of nodes on your method

public List<Node> populateDijkstraFrom(Node startNode) {
       // ^ Configure to return list instead of void

    // ......
    return nodeList;
}

public List<Node> getShortestPath(Node startNode, Node targetNode) {
    List<Node> pdList = populateDijkstraFrom(startNode);
    // ^ --- Get the list by simply passing the same parameter to the method
}

Another is stated by Gian

March 30, 2012

How to create a "listening(waiting?)" method

Question by Andrius Naruševičius

Lets say I have:

class X
{
    function a()
    {
        echo "Hello, ";
    }
    function b()
    {
        echo "John!";
    }
}

and then

Y = new X();
Y->a();

but once the method a is called, I also want the method b called immediately after it (so it kind of listens(waits?) till the moment when a is called and finished), so the output is

Hello, John

Is it possible to do that, and how that should look?
And no, calling $this->b(); at the end of method a is NOT a solution to what I want to do 🙂 Thanks in advance 🙂

Answer by Starx

You are searching for observer pattern. Read some example from the internet, you should be able to do what you are attempting.

However, A very simple example of using observer pattern:

class X
{
    private $observer;
    public function __construct() {
        $this -> observer = new XObserver();
    }

    function a() {
        echo "Hello,";      
        $this -> observer -> aExecuted($this);
    }

    function b() {
        echo "John!";
    }
}

class XObserver {

    public function aExecuted($obj) {
        return $obj -> b(); 
    }
}
March 25, 2012

Working with extended classes in PHP

Question by epic_syntax

I have 2 classes looking like this:

class db {

    protected $db;

    function __construct() {
        $this->connect();
    }

    protected function connect() {
        $this->db = new MySQLi(db_host, db_user, db_pass, db_name) or die($this->db->error);
        $this->db->set_charset('utf8');
    }

}

and

class sample extends db {

    protected $js_base_dir;

    public function __construct($js_base_dir = js_dir) {
        $this->js_base_dir = $js_base_dir . "/";
    }
 ....

I want to use $this->db inside second class, but __construct in sample class overrides first classes construct function. How to get $this-> db inside second class? Am I doung something wrong? if yes what’s proper way?

Answer by Starx

You can call parent class method, using parrent::methodName(). Similarly, you can use this to invoke parent’s constructor method as well as

parent::__construct();

Usage:

public function __construct($js_base_dir = js_dir) {
    parent::_construct();
    $this->js_base_dir = $js_base_dir . "/";
}

Apart from the manual read this article for extended explanation.

March 9, 2012

Instantiate an variable with a methods return object

Question by Eirc man

I have been searching for hours but I can’t seem to understand why this does not work. Here is the code

My goal is to have a different method to load the XML document, and another one to print and manage that document.

class ...

//Fetch and print xml document
    function fetchFromXMLDocument($XMLDocName) {
        $xmlDoc = new DOMDocument();
        $xmlDoc->load($XMLDocName);
        return $xmlDoc;

    }

Here I want do add the value of the fetchFromXMLDocument() to my $he variable.
but it does not seem to be working?

function printXml($XMLDocName) {
   //this seems not to be right??       
    $he = fetchFromXMLDocument($XMLDocName);

    //after that this is what I want to do..
    // $items = $he->getElementsByTagName("item");
         ...
    }

Does anybody have an idea on why that might be?

Answer by Starx

The problem is that function fetchFromXMLDocument(); is inside a class.

If the method you are accessing it from printXML, is within the same class then you should access it using $this operator.

function printXml($XMLDocName) {    
     $he = $this -> fetchFromXMLDocument($XMLDocName);
     ...
}

However, If the method you are accessing it from printXML, is outside. Then first you have to create and object of the class and access it.

function printXml($XMLDocName) {    
     $obj = new yourxmlclassname();
     $he = $obj -> fetchFromXMLDocument($XMLDocName);
     ...
}
...

Please fill the form - I will response as fast as I can!