Right now i am using
foreach (glob("app/view/components/*/*.php") as $filename) {
require_once $filename;
}
so there is automatically pick up every component as a function basically, but i like to use in Class so i can use like that:
Component::input($x);
but can't do that in class, if i do that
class Component{
foreach (glob("app/view/components/*/*.php") as $filename) {
require_once $filename;
}
}
I hope somebody help me i am currently using function. Please suggest some way to add function into a single class.
I used PowerShell and php script because not good enough in PowerShell
$Path =$PSScriptRoot;
$FileFilter = '*.php'
$IncludeSubfolders = $true
$AttributeFilter = [IO.NotifyFilters]::FileName, [IO.NotifyFilters]::LastWrite
try
{
$watcher = New-Object -TypeName System.IO.FileSystemWatcher -Property @{
Path = $Path
Filter = $FileFilter
IncludeSubdirectories = $IncludeSubfolders
NotifyFilter = $AttributeFilter
}
$action = {
$details = $event.SourceEventArgs
$Name = $details.Name
$FullPath = $details.FullPath
$OldFullPath = $details.OldFullPath
$OldName = $details.OldName
$ChangeType = $details.ChangeType
$Timestamp = $event.TimeGenerated
$global:all = $details
php C:\xampp816\htdocs\intaxingnew\php\app\view\components\compile.php
$text = "{0} was {1} at ford {2}" -f $FullPath, $ChangeType, $Timestamp
Write-Host ""
Write-Host $text -ForegroundColor DarkYellow
switch ($ChangeType)
{
'Changed' { "CHANGE" }
'Created' { "CREATED"}
'Deleted' { "DELETED"
Write-Host "Deletion Handler Start" -ForegroundColor Gray
Start-Sleep -Seconds 4
Write-Host "Deletion Handler End" -ForegroundColor Gray
}
'Renamed' {
$text = "File {0} was renamed to {1}" -f $OldName, $Name
Write-Host $text -ForegroundColor Yellow
}
default { Write-Host $_ -ForegroundColor Red -BackgroundColor White }
}
}
$handlers = . {
Register-ObjectEvent -InputObject $watcher -EventName Changed -Action $action
Register-ObjectEvent -InputObject $watcher -EventName Created -Action $action
Register-ObjectEvent -InputObject $watcher -EventName Deleted -Action $action
Register-ObjectEvent -InputObject $watcher -EventName Renamed -Action $action
}
$watcher.EnableRaisingEvents = $true
Write-Host "Watching for changes to $Path"
do
{
Wait-Event -Timeout 1
Write-Host "." -NoNewline
} while ($true)
}
finally
{
$watcher.EnableRaisingEvents = $false
$handlers | ForEach-Object {
Unregister-Event -SourceIdentifier $_.Name
}
$handlers | Remove-Job
$watcher.Dispose()
Write-Warning "Event Handler disabled, monitoring ends."
}
With php to write file
<?php
$y = fopen('../autoload.php', 'w');
$x = "<?php class Component {?>";
foreach (glob("*/*.php") as $filename) {
$x .= fread(fopen($filename, "r"), filesize($filename));
}
$x .= "<?php }";
$x = str_replace('?><?php', '', $x);
$x = str_replace('function ', 'public static function ', $x);
fwrite($y, $x);
It seem that good enough but still a hacky way to do thing.
My Answer:
To add functions from multiple PHP files into a single class in PHP, you can use the following approach:
1. Create a class called `Component` and define it as follows:
php
class Component {
public static function autoload() {
foreach (glob("app/view/components/*/*.php") as $filename) {
require_once $filename;
}
}
}
2. Call the `autoload` method of the `Component` class to load all the functions from the PHP files into the class:
php
Component::autoload();
By following this approach, all the functions from the PHP files in the specified directory will be loaded into the `Component` class, and you can access them using the `Component::functionName()` syntax. This way, you can organize your functions within a single class without the need for individual function definitions.
Rate this post
5 of 5 based on 5367 votesComments