Converting php code to Symfony class - php

i am using a simple php code with Activexpert to send sms here the intersting part
<?php
if(isset($_POST["submit1"]))
{
$_objSmsProtocolGsm = new Com("ActiveXperts.SmsProtocolGsm");
...
}
?>
it working fine but when i tried to insert it into my controller in symfony
i get an error
Attempted to load class "Com" from namespace
"PFE\SiivtBundle\Controller" in C:\Program Files
(x86)\EasyPHP-DevServer-14.1VC11\data\localweb\Symfony2.5\src\PFE\SiivtBundle\Controller\SiivtController.php
line 835. Do you need to "use" it from another namespace?
while my php code is simple and doesnt include or require any other files

Error message is self-explanatory.
Com class doesn't exist in PFE\SiivtBundle\Controller namespace (which is current namespace in your controller class).
Try:
new \Com("ActiveXperts.SmsProtocolGsm");
The backslash before classname means that we're looking for a class in global namespace

Related

Laravel Class 'App\FBLogin\authlogin' not found

I created a new FBLogin.php file in App Folder, added class:
namespace App\FBLogin;
class authlogin {}
Now i want to use this class in my Controller File, so i added:
use App\FBLogin\authlogin;
Now when i am trying to use this class authlogin, it is showing me error Class 'App\FBLogin\authlogin' not found
Is there something i am doing wrong?
Laravel Version: 5.5
Why would you use a lowercase format when naming your classes? Anyway, your namespace inside your app folder should follow your file structure.
If you create your class like below,
namespace App\FBLogin;
class authlogin {
// code here
}
Your file structure must be:
app/
FBLogin/
authlogin.php
Then you can use the class anywhere in your app by declaring the proper namespace
use App\FBLogin\authlogin;
$authlogin = new authlogin();

Is it correct to use PHP namespace declaration in 2 separate scripts?

I'm learning about PHP namespaces. I'm attempting to add a namespace to a class I wrote, and then reference it in another script. I've done it like this:
MenuBuilder.php
namespace Andytest\MenuBuilder;
class MenuBuilder {
public function test() {
echo 'testing';
}
}
test.php (where the class is used)
namespace Andytest\MenuBuilder;
require_once 'MenuBuilder.php';
$Builder = new MenuBuilder;
$Builder->test();
The output from this is as I expect - it outputs the word 'testing' when I run test.php.
But I'm not sure why I need namespace Andytest\MenuBuilder in test.php because it's already decleared in MenuBuilder.php, which is being required by test.php? If I remove the namespace line in test.php it doesn't work.
Am I doing this correctly?
In test.php you should have use Andytest\MenuBuilder\MenuBuilder; instead of namespace Andytest\MenuBuilder;
Because you use namespace instead of use the file expects every class you create that is not specified in the use case to be in the same namespace, therefore new MenuBuilder(); actually calls new Andytest\MenuBuilder\MenuBuilder();.
If you would change the namespace in test.php it should tell you that it cant find MenuBuilder

Symfony class not found but the class is defined

I've created a class in symfony, the file path is
src/Project/MemberBundle/Document/EnumMemberType.php
and the file looks like this
<?php
namespace Project\MemberBundle\Document;
class EnumMemberType {
const VIP = 'xxxxx';
} // class - EnumMemberType
but when i try to get the constant in this class in another file using the code
$memberType = \Project\MemberBundle\Document\EnumMemberType::VIP
i got the error
Attempted to load class "EnumMemberType" from namespace "Project\MemberBundle\Document".
Did you forget a "use" statement for another namespace?
I really can't figure out why, the file name is the same with the class name, and it's under the right folder, and the file starts with <?php.
Can anyone see what else i'm missing? Thanks
Maybe problem with namespace. You can write in the header of another file:
use Project\MemberBundle\Document\EnumMemberType
and
$memberType = EnumMemberType::VIP;

Access 3rd parties codes when using namespace PhP 5.5

I am developing with php 5.5, I provided code of my file here
<?php
require_once 'jsonrpcphp/includes/jsonRPCClient.php';
class Client
{
private $remoteMain;
public function __construct($param)
{
$this->remoteMain = new jsonRPCClient('http://
urlTofile/nameOfFile.php');
This codes works absolutely fine but the issue comes when I need to put a namespace for the file as soon as I put a namespace at the top of the file, for example:
<?php
namespace packagename\subPackage;
require_once 'jsonrpcphp/includes/jsonRPCClient.php';
class Client
This error will be displayed
Class 'packagename\subPackage\jsonRPCClient' not found in
The question is this:
how to access a class which is not in my namespace and provided from 3rd parties when I need to have namespaces
thanks in advance
If you're in namespace packagename\subPackage, then new jsonRPCClient refers to the class packagename\subPackage\jsonRPCClient. If you want to use a class from the global namespace, you need to explicitly specify that:
new \jsonRPCClient
Alternatively, explicitly alias the class at the top of the file:
use jsonRPCClient;
new jsonRPCClient(...)
Please RTM: http://php.net/manual/en/language.namespaces.basics.php

PHP namespace and include files

I have the following PHP code:
<?php
namespace my_ns;
class dummy
{
function do_print(){echo "printing...";}
}
$obj_dummy=new dummy();
$obj_dummy->do_print();
?>
This works all fine,
How ever If I put the class in a separate include file (e.g. class.dummy.php), making the code on the page look like below:
<?php
namespace my_ns;
include ("class.dummy.php");
$obj_dummy=new dummy();
$obj_dummy->do_print();
?>
I get the error message:
Class 'my_ns\dummy' not found in ...
How can by default make sure that (all) include-files are automatically added to a given namespace?
A file containing a namespace must declare the namespace at the top of the file before any other code - with one exception: the declare keyword.
Source
Add this to your included file as well.
namespace my_ns;
After that, your code works just fine.
Reference

Categories