{"id":2325,"library":"tree-sitter-php","title":"tree-sitter-php","description":"tree-sitter-php provides the PHP grammar for the tree-sitter parsing system. It enables incremental parsing to build concrete syntax trees for PHP source code, facilitating advanced code analysis, linting, and editor features. The library is actively maintained, with version 0.24.1 being the latest, and receives frequent updates to support new PHP language features.","status":"active","version":"0.24.1","language":"en","source_language":"en","source_url":"https://github.com/tree-sitter/tree-sitter-php","tags":["parsing","php","tree-sitter","grammar","AST","syntax-tree"],"install":[{"cmd":"pip install tree-sitter-php","lang":"bash","label":"Install with pip"}],"dependencies":[{"reason":"Provides the core Python bindings and parsing engine for tree-sitter grammars.","package":"tree-sitter","optional":false}],"imports":[{"note":"The 'language' function is typically accessed via the top-level package import to get the Language object.","symbol":"language","correct":"import tree_sitter_php"},{"note":"These are the core classes from the main tree-sitter Python bindings used to load and utilize the grammar.","symbol":"Language, Parser","correct":"from tree_sitter import Language, Parser"}],"quickstart":{"code":"from tree_sitter import Language, Parser\nimport tree_sitter_php\n\n# Load the PHP grammar\nPHP_LANGUAGE = Language(tree_sitter_php.language())\nparser = Parser(PHP_LANGUAGE)\n\nphp_code = b'''\n<?php\n\nclass MyClass {\n    public function __construct(private string $name) {}\n\n    public function greet(): string {\n        return \"Hello, \" . $this->name . \"!\";\n    }\n}\n\n$obj = new MyClass(\"World\");\necho $obj->greet();\n\n?>\n'''\n\n# Parse the code\ntree = parser.parse(php_code)\n\n# Get the root node of the syntax tree\nroot_node = tree.root_node\n\nprint(f\"Root node type: {root_node.type}\")\nprint(f\"Number of children: {len(root_node.children)}\")\n# Example: Find a class_declaration node\nfor child in root_node.children:\n    if child.type == 'declaration_list': # often wrappers around class/function\n        for decl in child.children:\n            if decl.type == 'class_declaration':\n                print(f\"Found class: {decl.child_by_field_name('name').text.decode('utf8')}\")\n                break\n\n# You can also use queries for more specific pattern matching\nquery = PHP_LANGUAGE.query(\"\"\"\n(class_declaration name: (name) @class-name)\n(method_declaration name: (name) @method-name)\n\"\"\")\n\ncaptures = query.captures(root_node)\nfor node, name in captures:\n    print(f\"Captured: {name.replace('-', '_')}: {node.text.decode('utf8')}\")","lang":"python","description":"This quickstart demonstrates how to load the PHP grammar, initialize a parser, and parse a sample PHP code snippet. It then shows how to access the root node of the generated syntax tree and perform a simple query to extract class and method names."},"warnings":[{"fix":"Ensure a C compiler (e.g., GCC or Clang) is installed and available in your PATH. For specific issues, consult the `py-tree-sitter` documentation or GitHub issues. Consider using `tree-sitter-languages` for a bundled solution if facing repeated compilation issues.","message":"Tree-sitter grammars are C libraries and typically require compilation. While `tree-sitter-php` provides pre-compiled binary wheels for common platforms, environments without a suitable C compiler or for non-standard architectures might encounter installation failures, requiring manual compilation.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Regularly update `tree-sitter-php` to the latest version to ensure compatibility with modern PHP syntax. Check release notes for specific PHP version support.","message":"The PHP grammar is continuously updated to support new language features (e.g., PHP 8.1+, 8.4, 8.5). Older versions of the `tree-sitter-php` grammar might fail to correctly parse newer PHP syntax, leading to incomplete or incorrect syntax trees.","severity":"breaking","affected_versions":"<0.24.0 (for PHP 8.5 pipe operator), <0.23.3 (for PHP 8.4 asymmetric property visibility), <0.22.x (for PHP 8.1+ features like `readonly`)"},{"fix":"Be aware of the distinction between the full PHP grammar (which handles HTML) and `php_only` variants if your toolchain offers them. Ensure proper language injection strategies in editors or tools. Consult `nvim-treesitter` or similar editor integration documentation for specific configurations related to mixed PHP/HTML files.","message":"Parsing `.php` files containing a mix of PHP and HTML can sometimes lead to issues or unexpected parsing behavior, especially when integrating with editor plugins that use multiple tree-sitter parsers (e.g., HTML and PHP). Historically, this led to a split into `php` and `php_only` parsers.","severity":"gotcha","affected_versions":"All versions, particularly when used in environments that auto-inject grammars for mixed content."}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}