I have the following code:
<?php
class iphone{
public $name;
public $email;
public function __construct($n , $e)
{
$this->name = $n;
$this->email= $e;
}
public function __clone()
{
$this->name = clone $this->name;
}
}
, and I want to write
$main = new iphone("zico" , "aa#a.a");
However when I try to type iphone VSCode transforms the class name into GlobalIphone and adds a use statement at the top of the code:
use iphone as GlobalIphone;
I want VSCode to write the name of the class without any additions, how do I make it do this?
As #underdog pointed out in the comments, the problem was that there were other classes with the same name in another php file:
When I changed the name of my class, the Global prefix was no longer automatically added by VSCode.
Related
Got a stucking situation which produces unnecessary IDE warnings and may lead to cleaning-up used code.
I think the best solution is to add some PHPDoc at the right place, but couldn't find the right place yet because of some constraints, as explained in the below examples.
IDE: PhpStorm
Result:
<?php
/*
* Generic result class, widely used, so can't touch it ¯\_(ツ)_/¯
*/
class Result {
public $data;
public function __construct() {
return $this;
}
public function setData($data) {
$this->data = $data;
return $this;
}
}
Customer:
<?php
class Customer {
public string $name = '';
public string $email = '';
public function __construct() {
$this->name = 'John Smith';
$this->email = 'test#example.com';
}
public function getCustomer(): Result {
return (new Result())->setData(new self());
}
public function reverseName(): string { // ❌ Unused element: 'reverseName'
$parts = explode(' ', $this->name);
return implode(' ', array_reverse($parts));
}
}
Controller:
<?php
require_once 'vendor/autoload.php';
$john = (new Customer())->getCustomer();
// ℹ️ $john->data is an instance of the Customer class.
// How should I tell to the IDE this ^ ❓
$reversedName = $john->data->reverseName(); // ❌ Cannot find declaration to go when trying to navigate to the method
exit($reversedName);
Tried many and many options, but the only one which works is by adding a PHPDoc to Result's $data property. Can't touch it because it's widely used in the project...
LE:: The Customer class has a lot of methods similar to reverseName(), so assigning the data property to a new variable is also difficult to write: /** #var Customer $john */ $john = (new Customer())->getCustomer()->data;
You could try to use another PhpStorm feature called advanced metadata.
Firstly you need to configure metadata in the next way
namespace PHPSTORM_META {
override(\App\Result::getData(0), map(['' => '#']));
}
then add getter to your Result class
public function getData()
{
return $this->data;
}
and use this getter in the next way
$john->getData(Customer::class)->reverseName();
The main idea is to use class name as an argument of the getter method and PhpStorm will know what class object this getter will return.
I started using OOP in PHP for first time, and I dont know how to achieve the following structure for my variables:
$this->myData->generalData->title;
$this->myData->generalData->description;
$this->myData->amount;
$this->myData->addNewData();
Up till now, what I am achieving is a normal variable inside a class:
$this->variablename
I tried doing this code, but it's not working at all:
$this->CDNE = "CDNE";
$this->CDNE->FOLDER = "hello man";
Can you explain me, how all this works?
Just to ilustrate my comment. Doing it with sub-objects could be something like this (a very basic example without attributes initialization):
class GeneralData{
public $title;
public $description;
}
class MyData{
public $generalData;
public $amount;
function __construct(){
$this->generalData = new GeneralData();
}
function addNewData(){
}
}
class MainClass{
public $myData;
function __construct(){
$this->myData = new MyData();
}
}
I am struggling with some PHP code here. Like the title says, I am trying to pass an instance of a Class as a parameter to a function (Note, this file is in another file and has a different scope, and thus I need to pass it as a parameter).
This is the code I have currently set up:
// requires built this page, the classes and the functions are in seperate files.
// That's why I need to pass it in as a parameter.
class ClassName {
private $name;
private $age;
public function getAge(){
return $this->age;
}
} $className = new ClassName();
// seperate file
var_dump($className); // works, but has no methods.
function randomFunc($className){
echo $className->getAge(); // call to undefined method className::getAge()
} randomFunc($className);
EDIT 2:
I have uploaded my code here:
http://sandbox.onlinephpfunctions.com/code/99954d61ca4d3f53ce549dab9f8333630633d89c
I hope you people can help me, any help would be much appreciated.
This works fine:
<?php
class ClassName {
private $name;
private $age = 19;
public function getAge(){
return $this->age;
}
}
function randomFunc($className){
echo $className->getAge(); // call to undefined method className::getAge()
}
$className = new ClassName();
randomFunc($className);
Outputs: 19
Your code also runs fine for me, it just outputs nothing since $age is empty.
What version of PHP are you running?
I have a class defined which has several constants defined through `const FIRST = 'something';
I have instantiated the class as $class = new MyClass()
then I have another class that takes a MyClass instance as one of it's constructors parameters and stores it as $this->model = $myClassInstance;
This works fine.
But I am wondering how I can access the constants from that instance?
I tried case $this->model::STATE_PROCESSING but my IDE tells me
Incorrect access to static class member.
and PHP tells me
unexpected '::' (T_PAAMAYIM_NEKUDOTAYIM) in ...
I know I can do MyClass::STATE_PROCESSING but I am wondering if there is a way to get them based off the instance?
Seems like your on an older version of php? PHP 5.3 allows the access of constants in the manner you describe... However, this is how you can do it without that inherent ability:
class ThisClass
{
const FIRST = 'hey';
public function getFIRST()
{
return self::FIRST;
}
}
class ThatClass
{
private $model;
public function setModel(ThisClass $model)
{
$this->model = $model;
}
public function getModel()
{
return $this->model;
}
public function Hailwood()
{
$test = $this->model;
return $test::FIRST;
}
}
$ThisObject = new ThisClass();
echo $ThisObject ->getFIRST(); //returns: hey
echo $ThisObject ::FIRST; //returns: hey; PHP >= 5.3
// Edit: Based on OP's comments
$ThatObject= new ThatClass();
$ThatObject->setModel($Class);
echo $ThatObject->getModel()->getFIRST(); //returns: hey
echo $ThatObject->Hailwood(); //returns: hey
Basically, were creating a 'getter' function to access the constant. The same way you would to externally access private variables.
See the OOP Class Constants Docs: http://php.net/manual/en/language.oop5.constants.php
I created two php classes separately. those are Student.php and Main.php this is my code.
this is my Student.php
<?php
class Student {
private $name;
private $age;
function __construct( $name, $age) {
$this->name = $name;
$this->age = $age;
}
function setName($name){
$this->name = $name;
}
function setAge($age){
$this->age = $age;
}
function getName() {
return $this->name;
}
function getAge() {
$this->age;
}
function display1() {
return "My name is ".$this->name." and age is ".$this->age;
}
}
?>
this is my Main.php
<?php
class Main{
function show() {
$obj =new Student("mssb", "24");
echo "Print :".$obj->display1();
}
}
$ob = new Main();
$ob->show();
?>
so my problem is when I call taht show method it gives Fatal error: Class 'Student' not found what is the wrong in here. is it necessary to import or something? please help me.
add
require_once('Student.php')
in your Main.php-file (on top) or before the inclusion of any other file...
The PHPUnit documentation says used to say to include/require PHPUnit/Framework.php, as follows:
require_once ('Student.php');
As of PHPUnit 3.5, there is a built-in autoloader class that will handle this for you:
require_once 'PHPUnit/Autoload.php'
You can use require_once('Student.php') or you can use PHP5 new feature namespaces for that. For a example assume your Student.php is in a dir called student. Then as the first line of Student.php you can put
<?php
namespace student;
class Student {
}
Then in your Main.php
<?php
use student\Student;
class Main {
}
It's worth taking a look at the PSRs. Particularly PSR-1
One of the recommendations is that
Files SHOULD either declare symbols (classes, functions, constants,
etc.) or cause side-effects (e.g. generate output, change .ini
settings, etc.) but SHOULD NOT do both
Following this guideline will hopefully make issues like the one you're having less common.
For example, it's common to have one file that is just in charge of loading all necessary class files (most commonly through autoloading).
When a script initializes, one of the first things it should do is to include the file in charge of loading all necessary classes.