Setting Up PHP Class's without a framework - php

Does anyone have a guide or can provide me with a quick example how to set up a class and pass the data from the class to the index.php?
I was thinking I could do it like I do in a framework can I not?
$this->class->function();

coolclass.php
class coolClass
{
public function getPrintables()
{
return "hello world!";
}
}
index.php
include 'coolclass.php';
$instance = new coolClass();
echo $instance->getPrintables();

A quick example:
Foo.php
class Foo {
public __construct() {
..initialize your variables here...
}
public function doSomething() {
..have your function do something...
}
}
Index.php:
Include('Foo.php');
$my_Foo = Foo();
$my_Foo->doSomething();
I think its fairly straight-forward as to what's happening here...so ill leave it at that. Don't want to give too much away.

Related

PHP Call a function from another function in same class

I am unable figure out how to make this work any help will be appreciated
<?php
class some{
function display()
{
$w ="its working";
$this->show($w);
}
function show($s)
{
echo $s;
}
}
?>
You were rightly advised to create an instance of your class then call the method on it but you said
see thats what i don't want .....i want some way to make it work without adding those two lines...by doing something else...just not that...and i can't figure out what i can do.
That something else is Simple! Make your method static
Declaring class properties or methods as static makes them accessible without needing an instantiation of the class.
public static function display()
{
$w ="its working";
self::show($w);
}
Then you can just do
some::display();
Fiddle
well it is working if you add the last two lines:
<?php
class some{
function display()
{
$w ="its working";
$this->show($w);
}
function show($s)
{
echo $s;
}
}
$x = new some;
$x->display();
?>
see here and click on "execute code"
Seems you are not called to display() function. Call to that function and try again.

Is there any way to get value from the previous class?

<?php
class A {
var $varA;
function funcA() {
$this->varA='AAAA';
$bbb = new B();
$bbb->funcB();
}
}
class B {
function funcB() {
//### Is there any way to get value of $varA from here?
}
}
echo $aaa->funcA;
?>
Please see the code above. Is there any way to get value of $varA from funcB()? I know about global and I know I can pass it or add var $varB; to the B class and set it. I am asking is there any standard way (syntax) to just get the value?
You could make the variable in class A public and static, which would make it accessible using A::$varA.
class A {
public static $varA;
function funcA() {
self::$varA = 'AAAA';
$bbb = new B();
$bbb->funcB();
}
}
class B {
function funcB() {
// Get value of $varA from A statically
echo A::$varA;
}
}
However, I wouldn't recommend doing it this way, as order of operations becomes more important. I'd look at traditional public getters/setters and take a look at possible desing flaws leading you to needing this solution in the first place.

Cross-class parameters

I have these 2 classes in 2 different files
class Foo {
public $type = int;
function __construct($out = 1) {
$this->type=$out;
}
public function get() {
return $this->type;
}
}
AND
class bar {
function __construct {
echo $foo->get();
}
}
maybe a dumb question but how come this is not working? In the above index.php file I have
$vFoo = new Foo(15);
$vBar = new Bar();
I though that Bar will echo Foo's type..
There is no $foo variable anywhere in your code and if it was, it would be out of scope. A class is not an object, learn the basics.
Probably what you want is:
class bar {
function __construct($foo) {
echo $foo->get();
}
}
and then in the index.php:
$vFoo = new Foo(15); $vBar = new Bar($vFoo);
But what you really need is not the solution - it's learning the basics. You don't seem to grasp what an object is and how it relates to a class (this is what your question implies; I am sure you believe otherwise).

Copy a function on the fly PHP

I'm working on a project which requires a function to be copied & executed on the fly and variables in it needs to be replaced on the fly too.
A simple example will be like this:
function myfunction()
{
$abc = $_SESSION['abc'];
return $abc;
}
I want to be able to call myfunction1() which does NOT physically exist in the code but does exactly the samething as the one above except it now take values from my custom variable so it'll look like this:
function myfunction1()
{
$abc = $myCustomVariable;
return $abc;
}
Any one help pls?
The more you describe how convoluted your function is, the more it sounds like a perfect candidate for an object with injected dependencies.
For instance, you could have (just going to describe the basic interfaces here):
class myClass
{
public function __construct($provider DataProvider)
{
$this->provider = $provider;
}
// Please name this something better
public function doStufferer()
{
if ($this->provider->hasParam('foo'))
{
return $this->provider->getParam('foo');
}
}
}
class SessionProvider implements DataProvider
{
// Session specific stuff
}
class OtherProvider implements DataProvider
{
// Other provider stuff
}
interface DataProvider
{
public function getParam($key);
public function hasParam($key);
public function setParam($key, $value);
}
You can then use it like this:
$dataProcessor = new myClass(new SessionProvider);
// OR $dataProcessor = new myClass(new OtherProvider);
$dataProcessor->doStufferer();
Please take a look at PHP Classes and Objects and the other related topics.
This is what parameters are for, I think your looking todo something like this:
$myCustomVariable = 'Some value';
function myfunction($var=$_SESSION['abc'])
{
$abc = $var;
return $abc;
}
myfunction(); //returns $_SESSION['abc']
myfunction($myCustomVariable); //returns "Some Value"
The direct answer is eval which I do not recommend.
You could have your function accept a parameter, like this.
function myfunction1($some_var)
{
$abc = $some_var;
return $abc;
}
// call it like...
myfunction1($myCustomVariable);
If you need to access a variable, but the name is generated by dynamic code, you can use $GLOBALS.
function myfunction1($name_of_var)
{
$abc = $GLOBALS[$name_of_var];
return $abc;
}
// call it like...
$myCustomVariable = 'a value'
myfunction1('myCustom' + 'Variable');

PHP add methods to Functions

Is it possible to add methods to functions?
For example:
<?
function func(){
;
}
//add method
func->test = function(){
;
}
func->test();
func();
I'm coming from a javascript background, and therefore I'm used to 'everything is an object'.
EDIT:
I was just explaining where the misconception may often come from for new phpers. I understand the above code doesn't work.
EDIT 2
Figured it out.
class myfunc_class{
function __invoke(){
//function body
}
function __call($closure, $args)
{
call_user_func_array($this->$closure, $args);
}
}
$func = new myfunc_class;
$func->test = function(){
echo '<br>test<br>';
};
$func->test();
$func();
Even sexier :)
class func{
public $_function;
function __invoke(){
return call_user_func_array($this->_function,func_get_args());
}
function __construct($fun){
$this->_function = $fun;
}
function __call($closure, $args)
{
call_user_func_array($this->$closure, $args);
}
}
$func = new func(function($value){
echo $value;
});
$func->method = function(){
echo '<br>test<br>';
};
$func('someValue');
$func->method();
No.
Not everything is an object in PHP. In fact the only thing that is an object is, well, an object. More specifically, and generally, an instantiation of a class.
Your code converted to PHP
// function_object.php
<?php
class FunctionObject {
public method func() {
// do stuff
}
}
?>
In other code you would use it like this:
<?php
// example.php in same folder as function_object.php
include 'function_object.php';
$FuncObj = new FunctionObject;
$FuncObj->func();
Also: read more about PHP & OOP
No, because an object is a different PHP language construct than a function. Functions do not have properties, but are instead simply execution instructions.
But, if func were instead a pre-defined class, then yes... with a bit of witchcraft, ignoring public outcry, foregoing readability and PHP coding standards, and by using closures with the __call() magic method...
class func
{
function __call($func, $args)
{
return call_user_func_array($this->$func, $args);
}
}
$obj = new func;
$obj->test = function($param1, $param2)
{
return $param1 + $param2;
};
echo $obj->test(1,1);
This won't work as you'd think without __call(), because by $obj->test(1,1), PHP thinks you're trying to call a non-existent method of func when out of object scope. But inside, being that the new "test" property is of a type: closure, the call_user_func_array() just sees the "test" property as just another function, so you can hide this bit of trickery from outside scope.
You would need your function func() to return an object, then you'd be able to do something like: func()->test();
But please note that your way of handling objects is not right in PHP and I suggest that you go read the OO documentations here.
In difference to javacript, in PHP not everything is an object. Therefore you need to differ between function and class.
If you want to create an object, you need to define the class first.
class myClass {
}
You can then add as many functions to the class as you need. But you need to define them first:
class myClass {
function test() {
echo "test!\n";
}
}
When everything is ready, you can bring it to life then:
$class = new myClass;
$class->test();
Checkout the manual for more.
You can't do what you're trying to do, but you can define functions inside of other functions.
This example outputs text:
function a() {
function b() { echo 'Hi'; }
}
a();
b();
Output: HiHi
This example outputs an error:
function a() {
function b() { echo 'Hi'; }
}
b();
Output: ERROR

Categories