Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Im currently learning oop from youtube etc and ive started my first project using it. The question i have that hasn't been answered along the way is..
say i have two classes
class a{
public function dosomething($var){
}
}
and
class b{
}
Can i access dosomething function from class b? if so could someone point me in the right direction.
Thanks
You have two options:
Pass instance of class a to class b and call method on that. (advisable)
Make method in class a static and call it like a::method(). (you should never do this)
To solve problem with the first way, your b class needs slight modification:
class b{
public function callMethodOfClassa(a $instanceOfa, $var) {
$instanceOfa->dosomething($var);
}
}
Or:
class b {
private $property;
public function callMethodOfClassa($var) {
$this->property->dosomething($var);
}
public function __construct(a $instanceOfa) {
$this->property = $instanceOfa;
}
}
In the second example you keep reference to passed instance in a field called $property here and is passed when instance of b is initialized:
$instanceOfa = new a();
$instanceOfb = new b($instanceOfa);
For better understanding of object oriented programming with php read the manual
And promised demo for the first example demo for the second sample (for better understanding made name changes).
Detailed explenation from comment:
class a{
public function dosomething(b $var){
$var->dosomething2();
}
public static function dosomething3(b $var){
$var->dosomething2();
}
}
and
class b{
public function dosomething2(a $var){
echo 'Hi, not doing anithing with this var!';
}
}
usage:
$variable1 = new a();
$variable2 = new b();
$variable1->dosomething($variable2);
a::dosomething3($variable2); //static call, no instance
$variable2->dosomething2($variable2);
Related
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
Is there a way to implement this idea
class Test{
public function test(){
return include 'test.php';
}
}
test.php
<?php
// process the functions here (including queries , etc)
return 1;
?>
Test
$test = new Test();
echo $test->test();
Basically, it's a combination of OOP and Procedural approach. The main goal here is to separate the process of the functions and put it on another file. The purpose is lessen the lines of the code from the class and make it easier to trace instead of scrolling to 1000+ lines of codes.
PS. I'm sorry for the title. I am not sure if this is possible but I hope you could give me idea.
Thanks and Have a nice day!
what you can do is to aggregate an other class (which can sit in an other file) and call a method of this:
Main Class: Test.php
<?php
use namespace\path\to\OtherClass;
//or require_once("path/to/OtherClass.php");
class Test
{
public function test()
{
$otherClass = new OtherClass();
return $otherClass->otherCall();
}
}
while the other class will look like this (OtherClass.php):
<?php
class OtherClass
{
public function otherCall()
{
return "some value";
}
}
Further ...
if you have a class which you want to mix in your classes, you can use the concept of mixins, which are called traits in php.
The OtherClass.php will change to
<?php
trait OtherClass
{
public function otherCall()
{
return "some value";
}
}
and the Test.php class would change to:
<?php
use namespace\path\to\OtherClass;
//or require_once("path/to/OtherClass.php");
class Test
{
use OtherClass;
public function test()
{
return $this->otherCall();
}
}
If you are new to OOP or Programming I can reccomand two books by Robert Martin:
"Clean Code" and "Clean Architecture".
And to get better in PhP I can recommand this website: https://phptherightway.com/
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
public function save(PropelPDO $con = null)
{
if ($this->isNew() && !$this->getExpiresAt())
{
$now = $this->getCreatedAt() ? $this->getCreatedAt('U') : time();
$this->setExpiresAt($now + 86400 * sfConfig::get('app_active_days'));
}
return parent::save($con);
}
I don't understand return parent::save($con) please help me, thanks
It's calling the parent method save so whatever class this one extends from it's calling that method.
class Animal {
public function getName($name) {
return "My fav animal: " . $name;
}
}
class Dog extends Animal {
public function getName($name) {
return parent::getName($name);
}
}
$dog = new Dog();
echo $dog->getName('poodle');
This class extends a Propel Model class which also has a save() method. This save method overrides the parent's save() method. When this overridden save() is invoked, it first does some work related to this concrete class, then invokes the parent's save() which will persist the object's properties in database.
If you look at the class declaration, it will say something like
class ThisClass extends anotherClass.
The line you don't understand is returning the output of the save() method in anotherClass
parent
is the keyword that means "the class that this class is extending"
the :: (Scope Resolution Operator) allows you to call that method -- provided it is declared as static without instantiating the class --
Unless there is something else going on, you should be able to replace that line with
return $this->save($con);
The :: is the scope resolution operator. It allows you to access properties or methods from a class.
The parent keyword refers to the parent class of the current class.
With that in mind, your return statement is calling the save() method of your class' parent.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
<?php
class foo
{
public $a;
public function __construct()
{
$a='value';
}
}
$class = new foo();
echo $class->$a;
?>
I want to use the value $a in other parts of my script.
How do I retrieve a variable set in a php object and use it for other things outside of the object?
To set the value inside a method, the syntax is:
$this->a = 'value';
To obtain the value of the property from an instance of the class, the syntax is:
$foo = new foo();
echo $foo->a;
The Properties manual page goes into more detail.
I'd recommend using a getter (see):
Inside your class.
private $a;
public function getA()
{
return $this->a;
}
Outside your class.
$class = new foo();
echo $class->getA();
Important: Inside your class you should refer to $a as $this->a.
Use $this variable to assign and reference instance variables and functions on the current object.
In your case replace $a = 'value'; with $this->a = 'value';
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am preparing for the ZEND Certified Engineer-Exam. Using the TestPassport-Engine "Virtual Exam", I came across this question:
Consider the following code. Which keyword should be used in line marked in bold to make this code work as intended?
abstract class Base {
protected function __construct() {}
public function create(){
// this line
return new self();
}
abstract function action();
}
class Item extends Base {
public function action () { echo __CLASS__; }
}
$item = Item::create();
$item->action();
And the correct answer is static. So, how should that look like in the end?
Simply change
public function create() {
return new self();
}
to
public static function create() {
return new static();
}
See here.
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 8 years ago.
Improve this question
is there any way who let the code below works?
public function my_function(My_Class $arg){ .... }
public class My_Sub_Class extends My_Class { ... }
//NOW
$my_object = new My_Sub_Class();
my_function($my_object);
I've Edited some write errors
public class My_Sub_Class extends My_Class() { }
should be
public class My_Sub_Class extends My_Class { }
Also, I'm not sure what $my_object = new My_Sub_Class(){ .... } is supposed to be. A My_Sub_Class is not a function!
Fixing these errors, adding a definition of My_Class to your testcase, and removing the public prefixes (because your example had no surrounding class definition), I end up with the following, which works just fine:
<?php
class My_Class {}
class My_Sub_Class extends My_Class {}
function my_function(My_Class $arg){ echo "my_function"; }
$my_object = new My_Sub_Class();
my_function($my_object);
?>
Take a look at the extends keyword in the PHP manual.
In fact, take a look at every feature you use in the manual, if you can't get something to "work".
You code works just fine (that is, passing a subclass to function that has a superclass typehint). Here's my code, verified to work on PHP 5.3.3:
<?php
class Foo {}
class Bar extends Foo {}
function quu(Foo $a) { return $a; }
$foo = new Foo();
$bar = new Bar();
var_dump(quu($foo));
var_dump(quu($bar));
Of course, you have a syntax error on your second and third line. But that has nothing to do with your question...
Since the child class extends parent class, it has to have all the properties and methods of the parent class. Because of that, you do not have to fear - your function will receive and work with objects of all the child classes as well.
You have an error in your third line - after initializing an object you do not need curly brackets.