variable ambit and sending parameters in php OO - php

I'm very confused about using the parameters through pages with PHP OO.
I'm following a tutorial about creating a framework (it's just like the Zend Framework); but, what I don't understand is when this happens:
Example, the index:
// File: sourcefiles/index.php
define('DS', DIRECTORY_SEPARATOR);
define('ROOT', realpath(dirname(__FILE__)).DS );
define ('APP_PATH',(ROOT.'aplicacion'));
require_once APP_PATH. DS.'Config.php';
require_once APP_PATH. DS.'Request.php';
require_once APP_PATH. DS.'BootStrap.php';
require_once APP_PATH. DS.'Controller.php';
require_once APP_PATH. DS.'View.php';
try
{
BootStrap::run(new Request());
I have:
// File: sourcefiles/controladores/IndexController.php
<?php
class IndexController extends Controller
{
public function __construct() {
parent::__construct();
}
public function indexAction()
{
$this->view->titulo='Homepage';
$this->view->contenido='Whatever';
$this->view->renderizar('index');
}
}
?>
And this:
// file : sourcefiles/aplicacion/View.php
<?php
class View
{
private $controlador;
private $layoutparams;
public function __construct(Request $peticion)
{
$this->controlador = $peticion->getControlador();
}
public function renderizar($vista,$item=false)
{
$rutaview = ROOT.'vistas'.DS.$this->controlador.DS.$vista.'.phtml';
if (is_readable($rutaview))
{
include_once $rutaview;
}
else
{
throw new Exception('Error de vista');
}
}
}
?>
And here is the View:
// file : sourcefiles/vistas/index/index.phtml
<h1>
Vista index..
<?php
echo $this->titulo;
echo $this->contenido;
?>
</h1>
Now my questions are:
How the IndexController can use the line? $this->view->titulo = blabla;
The view class doesn't have a "titulo" attribute; however, I can do that. But here is a curious thing, if I do that after calling the $this->view->renderizar('index'), I get the error.
How does the index.phtml file knows this? echo $this->titulo; because, there isn't a include or require called, it's confusing to me.
When I do a require or include call in a file, the required or included file knows the caller's variables?
If somebody can explain these to me, I would really appreciate it :D
or link me to a discussion on official information about this, or how is this called?

Think of an include or require line as "copy-and-pasting" the code from one file into another. This isn't quite accurate, but it explains part of the behaviour here:
In sourcefiles/aplicacion/View.php you include sourcefiles/vistas/index/index.phtml while inside the function View->renderizar. So all the code in index.phtml gets loaded as though it was happening inside that function too. This is why you can access $this, for instance.
As for referencing $this->view->titulo when you haven't defined it, this is PHP letting you be lazy. Just like any variable, a member on an object will spring into life as soon as you mention it, with only a notice warning that maybe you made a mistake.

Related

Namespaces and spl_autoload_register

I have found several SO questions similar to mine, but am struggling to find an answer that helps me, plus I'd really like to know the best practice for autoloading classes that exist within namespaces.
My folder structure:
root
-- classes
--- Users
---- Users.class.php
And users.php;
<?php
namespace CompanyName\ProjectName\Users;
class UserMapper
{
// class code here
}
And my autoload function, which sits in the root folder;
/* autoload classes on instatiation */
spl_autoload_register(function($class)
{
include $_SERVER['DOCUMENT_ROOT'] . '/classes/' . $class . '.class.php';
});
And, let's say I call the user class like so;
<?php
new \CompanyName\ProjectName\User();
Warning: include(/Applications/XAMPP/xamppfiles/htdocs/test_tool/classes/CompanyName\ProjectName\User.class.php): failed to open stream: No such file or directory in...etc
To use spl_autoload_register, do I need to map my folder structure to my namespace structure? I would prefer not to do this as I like to have my classes in the same folder, with sub folders within.
Or do I add extra code to my autoload function?
I have also searched the php manual, and there is no working namespace example, which I find very strange.
Any help would be much appreciated.
Thanks in advance.
Disclaimer: I am a beginer at php, my answer may not be correct
but i'm confident examining and testing the example bellow will help clarify the use of Namespaces with spl_autoload_register for any beginner like me.
Consider this folder structure :
root/ contains index.php.
root/model/ contains A.php & AA.php
A.php :
?php
namespace company\model;
use \company\model\A;
class A
{
public function speak()
{
echo 'hello world! ';
}
}
AA.php :
?php
namespace company\model;
use \company\model\A;
require_once 'A.php';
class AA extends A
{
public function shout()
{
echo 'HELLO WOORLD!!!';
}
}
index.php :
<?php
namespace company;
use \company\model\A;
function classLoader ($className)
{
if (file_exists($className.'.php'))
{
require_once $className.'.php';
} else {
$className = str_replace('\\', '/', $className);
$className = str_replace('company/', '', $className);
if (file_exists($className.'.php'))
require_once $className.'.php';
else
throw new EXCEPTION('classLoader could not find '.$className.'.php .');
}
}
spl_autoload_register(classLoader);
$obj = new A;
//we dont need to write ($obj = new \company\model\A;)
//because of statement at line 4
$obj->speak();
echo '<br/>';
$objA = new \company\model\AA;
$objA->shout();
echo '<br/>';
class AB extends \company\model\AA
{
public function doBoth()
{
$this->speak();
$this->shout();
}
}
$objB = new AB;
$objB->doBoth();

Is it possible to a pass function to an included file?

I have a view class with a function to build the view
class view {
//...
public function build() {
$view = $this;
$data = $this->resources->data;
function s($value) {
return \classes\tools\html\html::specialChars($value);
}
require $this->viewFile;
}
//...
}
And a view some view files
<?php var_dump($view);
// works fine, variables passed ok ?>
<?= s($data->someUnsafeString) ?>
<?php //Fatal error: Call to undefined function s() ?>
I could define the function s In each view file but I really dont want to have to do that.
I could pass the function as a variable $s=function(){..} but I'd prefer not too
I could call the static function in the view directly but even that is more long winded than I'd like:
<?= html::s($data->someUnsafeString) ?>
Is this possible? or any other suggestions?
If your project has a Front Controller (one file - entry point),
then you can to declare your function in common namespace in this or required(#require) by this file.
Common namespace is code without namespace or
namespace {
function s() {
}
}
You may be misunderstanding what you're doing there. Functions aren't scoped in PHP. Your function s(..) inside view::build is merely declaring a regular global function. Doing that inside functions isn't anything special. You can simply declare functions conditionally in PHP, e.g.:
if (!function_exists('s')) {
function s(..) ..
}
So just put that function declaration into a separate file and include it as needed; no need to define it again and again separately in each file.

PHP - call class from class

Hello i looking for a way to call class function from another class.
i've tried diffrent PHP ways such as the classic way, to call class from class.
http://pastebin.com/X5VfaChr
require_once "user.php";
$user = new UserAction();
class htmloutput{
public function WebSite(){
$user->Moshe();
}
}
Its shows me this error:" syntax error, unexpected 'new' (T_NEW), expecting function (T_FUNCTION)" i dont know about more ways to call a class.
I'll be happy to get helping and
learn somethin' from that.
Have Good day,
Baruch
Besides the comment from Abhik Chakraborty, this fixes the issue coming next:
It's all about scope. Google for DI injection:
require_once "user.php";
$user = new UserAction();
class htmloutput {
public function WebSite(UserAction $user) {
$user->Moshe();
}
}
Try to get the instance inside your function, as the following:
require_once 'user.php';
class htmloutput {
public function WebSite(){
$user = new UserAction();
$user->Moshe();
}
}

Can not call global function from php class

I have a php project that uses some functional elements, and some OOP elements, but it seems mixing the two is causing problems. Here are the files that are causing the errors:
DB.php
<?php
function parse_db_entry($from, &$to){
//Function code here
}
?>
User.php
<?php
require_once 'DB.php';
class User{
//Properties
public function __construct(){
//ctor
}
public static function load_user($email, $password){
$entry = //Make MySQL Request
$user = new User();
parse_db_entry($entry, $user);
return $user;
}
}
?>
Everything works as it should, except the call to parse_db_entry which throws:
Fatal error: Call to undefined function parse_db_entry()
I am able to access other things in DB.php, for instance if I made a class it there I am able to instantiate it without error, and if I move the function into User.php, it is functional as well. So what am I doing wrong? Why can't I call this method?
I've figured it out! Thanks to everyone who had ideas, but it seems the problem was something else.
When calling require_once 'DB.php', php was actually getting the file:
C:\xampp\php\pear\DB.php
instead of mine.
This may be a problem exclusive to XAMPP, but a simple rename of my file to DBUtil.php fixed everything.
This is a stretch, and I'm totally taking a shot in the dark here, but...
Are you sure parse_db_entry is in the global or User's namespace?
Note: I added a few lines here and there for testing/debugging.
DB.php:
<?php
namespace anotherWorld; // added this ns for illustrative purposes
function parse_db_entry($from, &$to){
echo 'called it';
}
?>
User.php:
<?php
namespace helloWorld; // added this ns for illustrative purposes
class User {
//Properties
public function __construct(){
//ctor
}
public static function load_user($email, $password){
$entry = //Make MySQL Request
$user = new User();
parse_db_entry($entry, $user);
return $user;
}
}
?>
test.php:
<?php
require_once 'DB.php';
require_once 'User.php';
use helloWorld\User;
$a = new User();
$a->load_user('email','pass');
echo 'complete';
?>
Yields Fatal error: Call to undefined function helloWorld\parse_db_entry() in User.php on line 13, however when removing the NS declaration in DB.php (namespace anotherWorld) thereby putting parse_db_entry in global NS it runs just fine.
To verify, use the __NAMESPACE__ constant.
If namespace is a problem, without compromising DB's namespace, here is an updated User.php:
<?php
namespace helloWorld;
use anotherWorld; // bring in the other NS
class User {
//Properties
public function __construct(){
//ctor
}
public static function load_user($email, $password){
$entry = //Make MySQL Request
$user = new User();
anotherWorld\parse_db_entry($entry, $user); // call the method from that NS
return $user;
}
}
?>

Cannot find Class with PHP Namespace

I posted some questions previously regarding the use of Namespaces in PHP and from what I got, this example code I have below should be working.
However I am getting errors when I try to use Namespace in PHP like this. Here is the first error when running the code below as is...
Fatal error: Class 'Controller' not found in E:\Controllers\testing.php on line 6
E:\Controller\testing.php File
<?php
use \Controller;
include('testcontroller.php');
$controller = new Controller;
$controller->show();
?>
E:\Controller\testcontroller.php File
<?php
use \Library\Registry;
namespace Controller
{
class Controller
{
public $registry;
function __construct()
{
include('E:\Library\Registry.class.php');
$this->registry = new Registry;
}
function show()
{
echo $this->registry;
echo '<br>Registry was ran inside testcontroller.php<br>';
}
}
}
?>
E:\Library\Registry.class.php File
<?php
namespace Library\Registry
{
class Registry
{
function __construct()
{
return 'Registry.class.php Constructor was ran';
}
}
}
?>
As you can see I tried to make it as simple as possible just to get the Namespace part working. I have tried different variations and cannot seem to figure it out.
Even when using use statement, you need to specify the namespace of the class you are trying to instantiate. There are a lot of examples here: http://www.php.net/manual/en/language.namespaces.importing.php
To understand it better, I will describe to you how it works. In your case, when you do use \Controller, the whole Controller namespace becomes available to you, but not the classes that are in this namespace. So, for example:
<?php
include('testcontroller.php');
use \Controller;
// Desired class is in namespace!
$controller = new Controller\Controller();
// Error, because in current scope there is no such class
$controller = new Controller();
$controller->show();
?>
Another example:
testcontoller.php:
<?php
namespace Some\Path\To\Controller;
class Controller
{
function __construct()
{
}
function show()
{
echo '<br>Was run inside testcontroller.php<br>';
}
}
?>
testing.php:
<?php
include('testcontroller.php');
use \Some\Path\To\Controller;
// We now can access Controller using only Controller namespace,
// not Some\Path\To\Controller
$controller = new Controller\Controller();
// Error, because, again, in current scope there is no such class
$controller = new Controller();
$controller->show();
?>
If you wish to import exactly the Controller class, you need to do use Controller\Controller - then this class will be accessible in your current scope.
Its not that good idea to name the namespace, like the class, because it is confusing (and I think this is what happens here). There moment you define the alias via use Controller this referenes to either a class \Controller, or the namespace \Controller, but your class, because it is within the namespace, is named \Controller\Controller 1
use Controller;
$class = new Controller\Controller;
or
$class = new \Controller\Controller;
or
use Controller\Controller;
$class = new Controller;
The idea is, that the moment you try to access a class with its relative name it tries to map the "first part" against any alias defined using use (remeber use MyClass is the same as use MyClass as MyClass. The thing after as is the alias).
namespace MyNamespace\MyPackage\SomeComponent\And\So\On {
class MyClass {}
}
namespace Another {
use MyNamespace\MyPackage\SomeComponent; // as SomeComponent
$class = new SomeComponent\An\So\On\MyClass;
}
As you can see PHP finds SomeComponent as the first part and maps it against the SomeComponent-alias the line above.
You can read more about it in the manual about namespaces.
1 Its called "Full-qualified classname", if you name a class with its complete name.
When you put a class Controller in the namespace Controller, then you have to reference it that way:
$controller = new Controller\Controller();
\Controller would be a class in the global (default) namespace, i.e. as if you used no namespace at all.
Strangely I have found that in my example code from the Question above, if I change all the Namespace's that are defined to something like MyLibrary so it would be like this code below...
E:\Library\Registry.class.php File
<?php
namespace MyLibrary
{
class Registry
{
function __construct()
{
echo 'Registry.class.php Constructor was ran';
}
}
}
?>
Then when I use use MyLibrary\Registry; in another file, I am able to access it how I had planned...
$this->registry = new Registry;
The reason this is very strange to me is this now makes a class name appear to be a Namespace as well. So I would not need to set a Namespace to 'MyLibrary\Library' to access the Registry instead I would do it like I showed in this answer to be able to access it with just calling the name of the class.
I hope this makes sense and helps someone else. I will not accept this as the answer as I am hoping someone with more know-how will come in and post a better Answer with explanation
try
<?php
use \Library\Registry;
namespace Controller;
class Controller
{
public $registry;
function __construct()
{
include('E:\Library\Registry.class.php');
$this->registry = new Registry;
}
function show()
{
echo $this->registry;
echo '<br>Registry was ran inside testcontroller.php<br>';
}
}
?>
and
<?php
namespace Library\Registry;
class Registry
{
function __construct()
{
return 'Registry.class.php Constructor was ran';
}
}
?>
First off, I believe you are using composer or composer is initialised in your project. If so, check composer.json file for your autoload, psr-4 definition. For example, if the root of your application is "App", then in your psr-4, you should be doing "autoload": { "psr-4": { "App\\": "./" } },
Furthermore, remember to clear composer cache and dump-autoload from the terminal as follows:
composer clear-cache
composer dump-autoload

Categories