Grant external files access to the object tree

王林
Release: 2024-02-08 23:15:22
forward
505 people have browsed it

Grant external files access to the object tree

In this article, php editor Shinichi will introduce how to grant access to the object tree to external files in PHP. In object-oriented programming, access permissions between objects are very important, as they can control the interaction and data sharing between objects. By using access modifiers (public, protected, private) in PHP, we can limit the access scope of objects and ensure the security and maintainability of the code. This article will explain the usage of these access modifiers in detail and provide some practical application examples to help readers deeply understand and apply these concepts. Whether you are a PHP beginner or an experienced developer, this article will provide you with valuable knowledge and practical tips. Let's explore the mysteries of object access permissions in PHP!

Question content

Assume that my module has the following .go file structure:

  • tree_definition.go
  • tree_creation.go

tree_creation parses some files and creates an object tree (otree for short) from them using the data structures and methods (and functions) defined in tree_definition.

During parsing, I see that the external.go file contains some code designed to access the otree and modify it.

Once the parsing is complete, the code in external.go should be able to use otree and modify it through the API I defined in tree_definition. This all happens at runtime.

clarify:

  • Yes, I'm trying to replicate the behavior of the DOM
  • external.go comes from another directory, not from inside the module
  • External.go is only visible at runtime when tree_creation adds external.go to otree's data structure

I've gone through almost all the steps to replicate it. This is the last part. Unfortunately I can't provide a link to Git at the moment so you can check it out for yourself, but I'd appreciate every comment that can help me clarify the situation.

Best regards!

I think I understand. I'll post it later.

Solution

Okay, let’s get started! Finally, with the help of my friend bing chat, I was able to find a simple solution without using memory files or making http requests through localhost. The solution proposed is very elegant and works perfectly for me! I still think I'll use memory files, but we'll see. It depends on scalability. In addition, special thanks to

@burakserdar

His answer.

package main

// This will be in the runtime file
import (
    "bufio"
    "fmt"
    "os/exec"
)

func main() {
    cmd := exec.Command("go", "run", "user_defined.go")
    stdout, err := cmd.StdoutPipe()
    if err != nil {
        panic(err)
    }
    err = cmd.Start()
    if err != nil {
        panic(err)
    }
    scanner := bufio.NewScanner(stdout)
    for scanner.Scan() {
        fmt.Println(scanner.Text())
        if scanner.Text() == "some_command" {
            fmt.Println("found some_command")
        }
        if scanner.Text() == "another_command" {
            fmt.Println("found another_command")
        }
    }
    err = cmd.Wait()
    if err != nil {
        panic(err)
    }
}
Copy after login
Edit 1 It turns out that there are 3 ways of communication between processes, but the one that works best for my problem is shared memory.

The above is the detailed content of Grant external files access to the object tree. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!