Teach you step by step how to do a keyword matching project (search engine) ---- Day 18, teach you how to do it on the 18th day_PHP Tutorial

WBOY
Release: 2016-07-13 10:19:53
Original
1178 people have browsed it

Teach you step by step how to do keyword matching projects (search engines) ---- Day 18, teach you how to do it Day 18

Day 18

Guest appearance: Diaosi’s deceptive form artifact

Things I’ve experienced: Database stuff

Starting point: Teach you step by step how to do keyword matching project (search engine) ---- Day 1

Review: Teach you step by step how to do keyword matching project (search engine) ---- Day 17

Last time, when Xiao Shuaishuai handed over the code to Boss Yu, Boss Yu criticized Xiao Shuaishuai, and Xiao Shuaishuai really felt wronged.

Let’s go back to looking at the problem from the perspective of the technical director. Does the technical director really want to supervise the readable lines of the code?

I remember that many companies advocate interests, and interests are used to measure the value of a technical director.

Did the technical director make a mistake? Is it beyond the scope of his duties?

In fact, when Boss Yu saw the three-layer foreach in the exec method of the LinklistCharListHandle class, he approved it.

Boss Yu is so strict, but Boss Yu has higher expectations for Xiao ShuaiShuai.

Xiao Shuai Shuai had no choice but to extract as much as possible from foreach. Xiao Shuai Shuai’s version:

<span>class</span> LinklistCharListHandle <span>extends</span><span> CharListHandle {
    </span><span>public</span> <span>function</span> <span>exec</span><span>(){
        </span><span>$sql</span> = "select word from category_linklist where cid='<span>$this</span>->selectorItem->cid'"<span>;
        </span><span>$linklist</span> = DB::makeArray(<span>$sql</span><span>);
        </span><span>foreach</span>(<span>$linklist</span> <span>as</span> <span>$strWords</span><span>){
            </span><span>$words</span> = <span>explode</span>(",",<span>$strWords</span><span>);

            </span><span>$this</span>->propertiesTransferCharlist(<span>$words</span><span>);

        }
    }

    </span><span>public</span> <span>function</span> propertiesTransferCharlist(<span>$linkWords</span><span>){
        </span><span>$properties</span> = <span>$this</span>->selectorItem-><span>getProperties();
        </span><span>foreach</span>(<span>$properties</span> <span>as</span> <span>$property</span><span>){

            </span><span>$this</span>->charlist->addCore(<span>$property</span>-><span>value);
            </span><span>if</span>(<span>in_array</span>(<span>$property</span>->value,<span>$linkWords</span><span>)){
                </span><span>$this</span>->addCores(<span>$linkWords</span><span>);
            }
        }
    }

    </span><span>public</span> <span>function</span> addCores(<span>$words</span><span>){
        </span><span>foreach</span>(<span>$words</span> <span>as</span> <span>$char</span><span>){
            </span><span>$this</span>->charlist->addCore(<span>$char</span><span>);
        }
    }
}</span>
Copy after login

Xiao Shuai Shuai extracted two methods to make the program more understandable.

In fact, Xiao Shuaishuai’s approach is to use refactoring-one of the techniques to improve the design of existing code, Extract Method to extract functions

Extract Method: Put this code into a separate function and let the function name resolve the purpose of the function.

Xiao Shuai Shuai felt very accomplished. When he continued to give the code to Boss Yu, Boss Yu mentioned two points.

1. propertiesTransferCharlist为什么要接受个参数。<br />2. addCores到底是那个类的职责范围。<br /><br /><br />
Copy after login

Xiao Shuaishuai knows how to do the second method. He moved the method into the Charlist class. The code is as follows:

<?<span>php
</span><span>class</span><span> CharList {

    </span><span>private</span> <span>$core</span> = <span>array</span><span>();
    </span><span>private</span> <span>$blacklist</span> = <span>array</span><span>();

    </span><span>public</span> <span>function</span> addCore(<span>$char</span><span>){
        </span><span>if</span>(!<span>in_array</span>(<span>$char</span>,<span>$this</span>-><span>core))
            </span><span>$this</span>->core[] = <span>$char</span><span>;
    }
    
    </span><span>public</span> <span>function</span> addCores(<span>$words</span><span>){
        </span><span>foreach</span>(<span>$words</span> <span>as</span> <span>$char</span><span>){
            </span><span>$this</span>->addCore(<span>$char</span><span>);
        }
    }

    </span><span>public</span> <span>function</span><span> getCore(){
        </span><span>return</span> <span>$this</span>-><span>core;
    }

    </span><span>public</span> <span>function</span> addBlacklist(<span>$char</span><span>){
        </span><span>if</span>(!<span>in_array</span>(<span>$char</span>,<span>$this</span>-><span>blacklist))
            </span><span>$this</span>->blacklist[] = <span>$char</span><span>;
    }

    </span><span>public</span> <span>function</span><span> getBlacklist(){
        </span><span>return</span> <span>$this</span>-><span>blacklist;
    }
}</span>
Copy after login

In fact, what Xiao Shuaishuai did this time was to use refactoring - one of the techniques to improve the design of existing code, Move Method to move functions.

Move Method: Create a new function with similar behavior in the class most commonly referenced by this function. Turn the old function into a pure delegate function, or remove the old function entirely.

Xiaoshuai really couldn’t figure out what to do with the first one, so he went to ask Boss Yu for advice. Boss Yu directly gave the code to Xiaoshuai. Boss Yu’s code is:

<?<span>php
</span><span>class</span> LinklistCharListHandle <span>extends</span><span> CharListHandle {
    
    </span><span>private</span> <span>static</span> <span>$linklist</span><span>; 
    
    </span><span>public</span> <span>function</span> <span>exec</span><span>(){
       </span><span>$this</span>-><span>propertiesTransferCharlist();
    }
    
    </span><span>public</span> <span>static</span> <span>function</span> linklist(<span>$cid</span><span>){
        </span><span>if</span>(!<span>isset</span>(self::<span>$linklist</span>) || !<span>isset</span>(self::<span>$linklist</span>[<span>$cid</span>]) || self::<span>$linklist</span>[<span>$cid</span>] == <span>null</span><span>){
            </span><span>$sql</span> = "select word from category_linklist where cid='<span>$cid</span>'"<span>;
            self</span>::<span>$linklist</span>[<span>$cid</span>] = DB::makeArray(<span>$sql</span><span>);
        }
        </span><span>return</span> self::<span>$linklist</span>[<span>$cid</span><span>];
    }

    </span><span>public</span> <span>function</span><span> propertiesTransferCharlist(){
        </span><span>$properties</span> = <span>$this</span>->selectorItem-><span>getProperties();
        </span><span>foreach</span>(<span>$properties</span> <span>as</span> <span>$property</span><span>){
            
            </span><span>$this</span>->charlist->addCore(<span>$property</span>-><span>value);            
            </span><span>$this</span>->extendCharlist(<span>$property</span>-><span>value);           
        }
    }
    
    </span><span>public</span> <span>function</span> extendCharlist(<span>$char</span><span>){
        </span><span>$linklist</span> = self::linklist(<span>$this</span>->selectorItem-><span>cid);
        </span><span>foreach</span>(<span>$linklist</span> <span>as</span> <span>$strWords</span><span>){
            </span><span>$words</span> = <span>explode</span>(",",<span>$strWords</span><span>);
            </span><span>if</span>(<span>in_array</span>(<span>$char</span>,<span>$words</span><span>)){
                </span><span>$this</span>->charlist->addCores(<span>$words</span><span>);
            }
        }
        
    }    
}</span>
Copy after login

Xiao Shuaishuai looked at it and sighed. It turns out that the code is changed casually, and the difference is so big. Why has he never felt this way before?

Xiao Shuai Shuai really hopes that he can be alone and not have to go to Boss Yu every day.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/870655.htmlTechArticleTeach you step by step how to do keyword matching projects (search engines) ---- Day 18, teach you how to do it Day 18 Guest appearance on Day 18: Diaosi’s deceptive form artifact. Things she has experienced: Database...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template