Accessing object details in PHP thru global variables [closed] - php

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I am new to PHP from the .net world and this is the code that i have,
class A
{
Album $album; // In C# .net i could have done this and assigned the display action album data to this
public function displayAction()
{
$form = new ButtonForm();
$id = (int)$this->params('id');
if (!$id) {
return $this->redirect()->toRoute('album', array('action'=>'add'));
}
$album = $this->getAlbumTable()->getAlbum($id);
return array(
'id' => $id,
'album' => $album,
'form' => $form
);
}
}
How can i do this is PHP ?
Consider that i come a pure object oriented technology. So help will be highly appreciated.

I know you can do type hinting in function arguments, but I am thinking that you can't with field variables of a class. Instead of Album $album; use public $album;. Then you can access it freely outside the object. e.g.
$obj = new A();
echo $obj->album;
Keep in mind though your var must be declared public to access it this way. private and protected will not allow access in the global scope. You will need to create accessor functions in that case.
EDIT
Also, whenever accessing field variables within your class use the $this keyword. So again, instead of $album you use $this->album.

Related

Best way to restrict return value from interface in PHP [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 months ago.
Improve this question
I am developing an open source project in Laravel. I want to create framework which people can create their own payment gateways for their needs by implementing generic interfaces and ui will interact with that payment gateway. Which is the best way restrict return value from interface.
Right now I'm using this technic:
interface PaymentGateway
{
public function savePaymentPlan($email, $name, $surname, $phone, $cardNum, $cardHolderName, $cardExpriy, $amount, $checkoutDay): SavePaymentPlanResult;
}
interface SavePaymentPlanResultInterface{
public function getCardToken();
public function setCardToken($token);
}
class SavePaymentPlanResult implements SavePaymentPlanResultInterface{
private $cardToken = null;
public function setCardToken($token){
$this->cardToken = $token;
}
public function getCardToken(){
return $this->cardToken;
}
}
And using all of them like that:
class StrapiPaymentGateway implements PaymentGateway{
public function savePaymentPlan($email, $name, $surname, $phone, $cardNum, $cardHolderName, $cardExpriy, $amount, $checkoutDay): SavePaymentPlanResult {
$savePaymentPlanResult = new SavePaymentPlanResult;
...
...
$savePaymentPlanResult->setToken('<some-token>')
...
...
return $savePaymentResult;
}
}
Inside controller
class Controller {
test(){
$strapiPaymentGateway = new StrapiPaymentGateway();
$token = $strapiPaymentGateway->getToken();
}
}
Is it true way to do that? Because so many stuff you have to do just restrict return value?
Thanks for your answer.
Yes, defining the return type in the interface is the best way to enforce it.
However, if you want to be strict about coding towards an interface the savePaymentPlan method should return the SavePaymentPlanResultInterface instead of the concrete type. Then again, if you know you will only have one SavePaymentPlanResult, you don't necessarily need the SavePaymentPlanResultInterface.
On a side note: your naming is a bit inconsistent. You seem to add the Interface for interfaces, but then PaymentGateway interface is just called PaymentGateway.

Multiple Method Access Inside Class PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I'm new in PHP OOP and curious about method. How to make method inside method (I dont know what its name)?
For example, I can access like this
<?php
$myClass = new CarClass;
$myClass->createNew->bodySection->setColor("red");
Just like Codeigniter for calling a Models or Library using this.
<?php
$this->myLibrary->getData()
It's different from method chaining where between method call there is no parameter, its like javascript.
Can I achieve that? Or any alternative?
Thank you
Given the code,
$myClass = new myCar;
$myClass->createNew->bodySection->setColor("red");
we can make the following statements:
myCar has a property named “createNew”.
createNew holds some unknown object
The unknown object has a property called bodySection
The property named bodySection contains an unknown object that has a method named setColor()
Clear as mud?
There are several ways this could be illustrated; here’s one:
class myCar {
public createNew;
public function __construct() {
$this->createNew = new Foo;
}
}
class Foo {
public bodySection;
public function __construct() {
$this->bodySection = new Bar;
}
}
class Bar {
public function setColor($color) {
echo "Color is $color";
}
}
$myClass = new MyClass;
$myClass->createNow->bodySection->setColor('red');
// output: Color is red
The first problem here is that “createNow” doesn’t make sense as a property; it’s an action, not something that a myCar would own or do.
Likewise, a bodySection would probably have a color as a property, to be set with its own setter method, not some external object.
Bottom line, making long chains of pointers is not something to seek after; rather, they’re probably better kept as short as possible. Otherwise your object probably knows too much about to many things.

How to handle data when using classes - PHP [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am currently working within CodeIgniter and I am struggling to understand how to properly use Anemic or Rich Entity Classes within my programming.
I am looking to create an anemic entity that has getters/setters, but I am wondering how this all works.
For instance:
I can use the object to set and get information while an instance of the object is being created for the first time, but when retrieving information from my database how do I return the information into an object so that I can use the objects methods?
I have heard of using Doctrine to map objects to a relational database but I have already created the database schema I want to use and I am unsure about getting Doctrine to map to an existing database.
I have thought about trying:
Using the __construct function to set all of the objects properties
using the array I get from the SQL query, which seems improper.
Creating an instance of the object and setting all the objects
properties to the values of the array I get from the SQL query, which seems tedious.
Creating a method within the object that takes the array I get from
the SQL query and assigns all of the values to the proper object
properties, which seems okay.
But my question is: Is there a universally accepted practice for doing this? (other than Doctrine)
I would suggest using Doctrine or other ORM tool, but if you want to DIY, here is small example of using reflection.
<?php
class Foo {
private $id;
private $name;
public function getId(){ return $this->id; }
public function getName(){ return $this->name; }
public function setName($name){ return $this->name = $name; }
}
$dataFromDb = [
[
'id' => 1,
'name' => 'John'
]
];
foreach ($dataFromDb as $row) {
$foo = new Foo();
$ref = new ReflectionClass('Foo');
foreach ($row as $propName => $propValue) {
$prop = $ref->getProperty($propName);
$prop->setAccessible(true);
$prop->setValue($foo, $propValue);
}
var_dump($foo);
}

How can I get a public variable in PHPOO? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to put a 'global' value in a PHP controller, because I use it constantly in different functions, so, I've to send it a lot of times in the view, I saw that I can do a public variable like an attribute in Java.
I have to declare the variable:
var $team="";// or public $team="";
How can I put, and get the value of the PHP attribute?
Class Example:
class MyClass
{
private $_a;
public $b;
public function __construct()
{
// example of constructor
}
public function set($a)
{
$this->_a = $a;
}
public function get()
{
return $this->_a;
}
}
Example of usage:
// usage construct class and invoke private function set();
$myExampleClass = new MyClass();
$myExampleClass->set('something');
// set public
$myExampleClass->b = "i set a public var";
It probably best to design your class with getters and setters so keeping your variables private, unless you have specific reasons. Here is a good reference for best practices and styleguide.
I hope this helps.

PHP Classes:: Why declare as a new? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I'm very new to php classes and I was wonder why do I need to declare it to a variable and set it as NEW?
Here is an example :
class myFirstClass {
function Version(){
return 'Version 1';
}
function Info(){
return 'This class is doing nothing';
}
}
$hola = new myFirstClass;
echo $hola->Version();
Why this won't work WITHOUT declare it to a variable and set it to NEW ?
In other words... Without this line :
$hola = new myFirstClass;
I'm so used to functions so it looks weird to me...
This is a basic principle of Object Oriented Programming (OOP). Let's use a library system for example. If you want to get the name of a book, you cannot just say "give me the name of the book", you have to know what book (by id, author, whatever).
In functions, you can write one that looks like this:
function get_book($id){ // code }
In OOP it doesn't really work that way. You have a class book that keeps a name. But that name is only for that given book.
class Book {
var $name;
public __construct($name) {
$this->name = $name;
}
public function getName() {
return $this->name;
}
}
In order to call the getName() function we need to have a book. This is what new does.
$book = new Book("my title");
Now if we use the getName() function on $book we'll get the title.
$book->getName(); // returns "my title"
Hope that helps.
You are right! It is not necessary to use the new operator:
class myFirstClass {
static function Version(){// static keyword
return 'Version 1';
}
function Info(){
return 'This class is doing nothing';
}
}
echo myFirstClass::Version();// direct call
To create an instance of a class, the new keyword must be used. An object will always be created unless the object has a constructor defined that throws an exception on error. Classes should be defined before instantiation (and in some cases this is a requirement).
If a string containing the name of a class is used with new, a new instance of that class will be created. If the class is in a namespace, its fully qualified name must be used when doing this.
The Basics
http://php.net/manual/en/language.oop5.basic.php
Classes and Objects
http://php.net/manual/en/language.oop5.php
This line:
$hola = new myFirstClass;
Is saying: create a new object called $hola, then put a new instance of myFirstClass into $hola. Now $hola is literally a object containing a new instance of myFirstClass.

Categories